Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed runtime error occurring when calling v2 API on v1 serve #220

Merged
merged 3 commits into from
Dec 15, 2020

Conversation

vlastahajek
Copy link
Contributor

Closes #216

Proposed Changes

Properly handling unexpected responses in generated code

Checklist

  • CHANGELOG.md updated
  • Rebased/mergeable
  • A test has been added if appropriate
  • Tests pass
  • Commit messages are in semantic format

Copy link
Contributor

@sranka sranka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, requiring >=300 status code for a successful API response does the fix. It might have been the case that the error body was exposed from the generated API intentionally, for example here: https://github.com/influxdata/influxdb-client-go/blob/master/api/buckets.go#L148 . I guess that such bodies are now lost and different errors are thrown, don't they?

@vlastahajek
Copy link
Contributor Author

Added default switch clause is executed only if none of defined response types are not handled, e.g. for mentioned CreateBucket:

    switch {
	case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 201:
		var dest Bucket
		if err := json.Unmarshal(bodyBytes, &dest); err != nil {
			return nil, err
		}
		response.JSON201 = &dest

	case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 422:
		var dest Error
		if err := json.Unmarshal(bodyBytes, &dest); err != nil {
			return nil, err
		}
		response.JSON422 = &dest

	case strings.Contains(rsp.Header.Get("Content-Type"), "json") && true:
		var dest Error
		if err := json.Unmarshal(bodyBytes, &dest); err != nil {
			return nil, err
		}
		response.JSONDefault = &dest

	// Fallback for unexpected error
	default:
		if rsp.StatusCode > 299 {
			return nil, &ihttp.Error{StatusCode: rsp.StatusCode, Message: rsp.Status}
		}
	}

@vlastahajek vlastahajek merged commit bedbac9 into influxdata:master Dec 15, 2020
@vlastahajek vlastahajek deleted the fix/api-panics branch December 15, 2020 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GetAuthorization returns panic
2 participants