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 HTTP error handling in RESTClient.Request() method #82

Merged
merged 2 commits into from Oct 14, 2022

Conversation

mislav
Copy link
Contributor

@mislav mislav commented Oct 13, 2022

  • Ensure that Request() calls HandleHTTPError so that any JSON error body properly gets parsed;
  • Ensure that response body is closed after reading the error response.

Ref. cli/cli#6429

- Ensure that `Request()` calls HandleHTTPError so that any JSON error body properly gets parsed;
- Ensure that response body is closed after reading the error response.
Copy link
Contributor

@samcoe samcoe left a comment

Choose a reason for hiding this comment

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

LGTM, just left one comment about some code simplifying. Thanks for cleaning this up.

return api.HandleHTTPError(resp)
}

if resp.StatusCode == http.StatusNoContent {
return nil
}
defer resp.Body.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

If we move lines 70-72 above line 64 we can remove one of the defer resp.Body.Close() lines.

Copy link
Contributor Author

@mislav mislav Oct 14, 2022

Choose a reason for hiding this comment

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

In case of HTTP 204 No Content, resp.Body will be nil. This is why I'm closing the body in two different places— it's being consumed in two different places too

@mislav mislav merged commit c2fc965 into trunk Oct 14, 2022
@mislav mislav deleted the request-http-error branch October 14, 2022 10:51
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.

None yet

2 participants