From 072ab47aa0940cd2edf730d39e97aaa8deef7ca2 Mon Sep 17 00:00:00 2001 From: Sarah Vessels Date: Mon, 23 Sep 2019 15:56:18 -0500 Subject: [PATCH 1/2] Do response code checks earlier --- graphql.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/graphql.go b/graphql.go index 05c29b7..52bc8ef 100644 --- a/graphql.go +++ b/graphql.go @@ -131,6 +131,9 @@ func (c *Client) runWithJSON(ctx context.Context, req *Request, resp interface{} 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 { @@ -138,9 +141,6 @@ func (c *Client) runWithJSON(ctx context.Context, req *Request, resp interface{} } 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 { @@ -202,6 +202,9 @@ 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 { @@ -209,9 +212,6 @@ func (c *Client) runWithPostFields(ctx context.Context, req *Request, resp inter } 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 { From 8386eba72a31a1f9bc3ad9336ad8f0ad5f3b8c08 Mon Sep 17 00:00:00 2001 From: Sarah Vessels Date: Mon, 23 Sep 2019 16:20:22 -0500 Subject: [PATCH 2/2] Update expected error messages --- graphql_json_test.go | 2 +- graphql_multipart_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/graphql_json_test.go b/graphql_json_test.go index a973d2d..6c85d30 100644 --- a/graphql_json_test.go +++ b/graphql_json_test.go @@ -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) { diff --git a/graphql_multipart_test.go b/graphql_multipart_test.go index b52da2c..3b66885 100644 --- a/graphql_multipart_test.go +++ b/graphql_multipart_test.go @@ -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) {