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

Do response code checks earlier #47

Closed
wants to merge 2 commits into from
Closed

Do response code checks earlier #47

wants to merge 2 commits into from

Conversation

cheshire137
Copy link

This builds on @erutherford's change in #19 by moving the status code checks earlier. The library was only checking the response code if there was a problem decoding the response body as JSON, but a non-200 response from the API indicates a problem even if the response body was valid JSON. For example, the GitHub GraphQL API will return the valid JSON {"message":"Bad credentials","documentation_url":"https://developer.github.com/v4"} if you give bad credentials.

cc @matryer 🙇‍♀

@@ -131,16 +131,16 @@ func (c *Client) runWithJSON(ctx context.Context, req *Request, resp interface{}
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
Copy link

Choose a reason for hiding this comment

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

This should come after defer res.Body.Close(). As mentioned in the http.Client.Do docs:

If the returned error is nil, the Response will contain a non-nil Body which the user is expected to close.

Similar below.

@necrophonic
Copy link

Is there any chance we could return the response from the server, potentially as part of the error, if there was one?

In your case above (and actually the exact issue I'm having!) it would be helpful to have the message from the server as well as the status code error.

@saopayne
Copy link

When can we have this in a release, please?

return fmt.Errorf("graphql: server returned a non-200 status code: %v", res.StatusCode)

We need this check and it's not available in V2.2. Can master be promoted to a release?

@marwan-at-work
Copy link

I think it would be great if we used Go 1.13 primitives to return a friendly error response while still being able to get the un-modified response body and status code from the user side, something like this:

type StatusError struct {
  Code int
  Body []byte
}

func (se *StatusError) Error() string {
  return fmt.Sprintf("unexpected response code: %d", se.Code)
}

// later on
if resp.StatusCode != 200 {
  body, _ := ioutil.ReadAll(resp.Body)
  return &StatusError{Code: resp.StatusCode, Body: body}
}

This way, a user can see a nicely formatted error but also be able to inspect both the code and the body by doing the following:

import "errors"

var statusErr *graphql.StatusError
if errors.As(&statusErr) {
  json.Unmarshal(statusErr.Body, &myCustomStruct) // etc etc
}

Just FYI, users of Go version 1.12 and lower can still leverage the code above by just type-casting the error value.

@sminf
Copy link

sminf commented May 8, 2020

According to @marwan-at-work's proposal, I wrote a GraphQL client for better error handling.
poohvpn/gqlgo

@JanDonnermayer
Copy link

According to @marwan-at-work's proposal, I wrote a GraphQL client for better error handling.
poohvpn/gqlgo

Thx! This client is much better, yet better documented

@cheshire137
Copy link
Author

I found this PR still lingering, I'm going to close it out as stale. Thanks all!

@cheshire137 cheshire137 closed this Dec 6, 2022
@cheshire137 cheshire137 deleted the check-status branch December 6, 2022 22:58
@matryer
Copy link
Contributor

matryer commented Dec 6, 2022 via email

@karelbilek
Copy link

Who can control the repo? It should be more clearly stated that it's unmaintained

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.

9 participants