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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

return fmt.Errorf("graphql: server returned a non-200 status code: %v", res.StatusCode)
}
defer res.Body.Close()
var buf bytes.Buffer
if _, err := io.Copy(&buf, res.Body); err != nil {
return errors.Wrap(err, "reading body")
}
c.logf("<< %s", buf.String())
if err := json.NewDecoder(&buf).Decode(&gr); err != nil {
if res.StatusCode != http.StatusOK {
return fmt.Errorf("graphql: server returned a non-200 status code: %v", res.StatusCode)
}
return errors.Wrap(err, "decoding response")
}
if len(gr.Errors) > 0 {
Expand Down Expand Up @@ -202,16 +202,16 @@ func (c *Client) runWithPostFields(ctx context.Context, req *Request, resp inter
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("graphql: server returned a non-200 status code: %v", res.StatusCode)
}
defer res.Body.Close()
var buf bytes.Buffer
if _, err := io.Copy(&buf, res.Body); err != nil {
return errors.Wrap(err, "reading body")
}
c.logf("<< %s", buf.String())
if err := json.NewDecoder(&buf).Decode(&gr); err != nil {
if res.StatusCode != http.StatusOK {
return fmt.Errorf("graphql: server returned a non-200 status code: %v", res.StatusCode)
}
return errors.Wrap(err, "decoding response")
}
if len(gr.Errors) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion graphql_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestDoJSONBadRequestErr(t *testing.T) {
var responseData map[string]interface{}
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
is.Equal(calls, 1) // calls
is.Equal(err.Error(), "graphql: miscellaneous message as to why the the request was bad")
is.Equal(err.Error(), "graphql: server returned a non-200 status code: 400")
}

func TestQueryJSON(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion graphql_multipart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestDoBadRequestErr(t *testing.T) {
defer cancel()
var responseData map[string]interface{}
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
is.Equal(err.Error(), "graphql: miscellaneous message as to why the the request was bad")
is.Equal(err.Error(), "graphql: server returned a non-200 status code: 400")
}

func TestDoNoResponse(t *testing.T) {
Expand Down