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 API Error when error body is empty #68

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

codetruelle
Copy link

When an API call is returning a bad status code (!=200) and no body, the client return the body read error EOF instead of the error code. It's more difficult to debug and understand what happened.
A call to POST /cloud/project/*/instance with a bad or empty image id will return EOF.
With this patch, it return the 404 error saying an object is not found.
It's not perfect as the API should return the exact error, but more useful than EOF.

@deathiop
Copy link
Contributor

deathiop commented Apr 14, 2023

Hi there,

Thanks for contributing!
As per our contributing policy, may I please ask you to sign-off you commit? (git commit --amend -s --no-edit && git push -f).

Also, please squash these 4 commits.

@@ -397,24 +397,36 @@ func (c *Client) CallAPIWithContext(ctx context.Context, method, path string, re
// UnmarshalResponse checks the response and unmarshals it into the response
// type if needed Helper function, called from CallAPI
func (c *Client) UnmarshalResponse(response *http.Response, resType interface{}) error {
// Read all the response body
defer response.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.

This should stay here.

@@ -397,24 +397,36 @@ func (c *Client) CallAPIWithContext(ctx context.Context, method, path string, re
// UnmarshalResponse checks the response and unmarshals it into the response
// type if needed Helper function, called from CallAPI
func (c *Client) UnmarshalResponse(response *http.Response, resType interface{}) error {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func (c *Client) UnmarshalResponse(response *http.Response, resType interface{}) error {
func (c *Client) UnmarshalResponse(response *http.Response, resType interface{}) error {
// Read all the response body
defer response.Body.Close()

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.

3 participants