From a5b093eb0610ba4072ad763f94b0017a847c7331 Mon Sep 17 00:00:00 2001 From: Luke Manson Date: Thu, 20 Jun 2024 14:51:21 +0100 Subject: [PATCH 1/5] returns ErrResponseStatusNotOK when the request response returns a non-200 http status code --- graphql.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/graphql.go b/graphql.go index 706092d..7f88c6c 100644 --- a/graphql.go +++ b/graphql.go @@ -178,8 +178,11 @@ func (c *Client) request(ctx context.Context, query string, variables map[string } if resp.StatusCode != http.StatusOK { - body, _ := io.ReadAll(resp.Body) - err := newError(ErrRequestError, fmt.Errorf("%v; body: %q", resp.Status, body)) + err := newError(ErrRequestError, ErrResponseStatusNotOK{ + body: resp.Body, + status: resp.Status, + statusCode: resp.StatusCode, + }) if c.debug { err = err.withRequest(request, reqReader) @@ -386,6 +389,28 @@ func newError(code string, err error) Error { } } +type ErrResponseStatusNotOK struct { + body io.ReadCloser + status string + statusCode int +} + +func (e ErrResponseStatusNotOK) Error() string { + return fmt.Sprintf("non-200 OK status code: %s", e.status) +} + +func (e ErrResponseStatusNotOK) Body() io.ReadCloser { + return e.body +} + +func (e ErrResponseStatusNotOK) Status() string { + return e.status +} + +func (e ErrResponseStatusNotOK) StatusCode() int { + return e.statusCode +} + func (e Error) withRequest(req *http.Request, bodyReader io.Reader) Error { internal := e.getInternalExtension() bodyBytes, err := io.ReadAll(bodyReader) From 91d0bc72e7d39dc9863ab4c2f92b09512135ebfc Mon Sep 17 00:00:00 2001 From: Luke Manson Date: Thu, 20 Jun 2024 16:07:11 +0100 Subject: [PATCH 2/5] removed body, status, and added message to error type --- graphql.go | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/graphql.go b/graphql.go index 7f88c6c..8058858 100644 --- a/graphql.go +++ b/graphql.go @@ -178,9 +178,8 @@ func (c *Client) request(ctx context.Context, query string, variables map[string } if resp.StatusCode != http.StatusOK { - err := newError(ErrRequestError, ErrResponseStatusNotOK{ - body: resp.Body, - status: resp.Status, + err := newError(ErrRequestError, NetworkError{ + message: "non-200 OK status code", statusCode: resp.StatusCode, }) @@ -389,25 +388,16 @@ func newError(code string, err error) Error { } } -type ErrResponseStatusNotOK struct { - body io.ReadCloser - status string +type NetworkError struct { + message string statusCode int } -func (e ErrResponseStatusNotOK) Error() string { - return fmt.Sprintf("non-200 OK status code: %s", e.status) +func (e NetworkError) Error() string { + return fmt.Sprintf("%d %s: %s", e.statusCode, http.StatusText(e.statusCode), e.message) } -func (e ErrResponseStatusNotOK) Body() io.ReadCloser { - return e.body -} - -func (e ErrResponseStatusNotOK) Status() string { - return e.status -} - -func (e ErrResponseStatusNotOK) StatusCode() int { +func (e NetworkError) StatusCode() int { return e.statusCode } From dd9a2adc9f8ad28dd60f54cb61baa0b755ac397f Mon Sep 17 00:00:00 2001 From: Luke Manson Date: Fri, 21 Jun 2024 16:05:45 +0100 Subject: [PATCH 3/5] remove message field, add body field with accessor --- graphql.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/graphql.go b/graphql.go index 8058858..d72264f 100644 --- a/graphql.go +++ b/graphql.go @@ -178,9 +178,10 @@ func (c *Client) request(ctx context.Context, query string, variables map[string } if resp.StatusCode != http.StatusOK { + b, _ := io.ReadAll(resp.Body) err := newError(ErrRequestError, NetworkError{ - message: "non-200 OK status code", statusCode: resp.StatusCode, + body: string(b), }) if c.debug { @@ -389,12 +390,16 @@ func newError(code string, err error) Error { } type NetworkError struct { - message string + body string statusCode int } func (e NetworkError) Error() string { - return fmt.Sprintf("%d %s: %s", e.statusCode, http.StatusText(e.statusCode), e.message) + return fmt.Sprintf("%d %s", e.statusCode, http.StatusText(e.statusCode)) +} + +func (e NetworkError) Body() string { + return e.body } func (e NetworkError) StatusCode() int { From c4d2b7b1639186695f47d691a2f6815f6e73c34e Mon Sep 17 00:00:00 2001 From: Luke Manson Date: Fri, 21 Jun 2024 16:44:55 +0100 Subject: [PATCH 4/5] fix graphql_test.go --- graphql_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graphql_test.go b/graphql_test.go index 224e926..87233d1 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -215,7 +215,7 @@ func TestClient_Query_errorStatusCode(t *testing.T) { if err == nil { t.Fatal("got error: nil, want: non-nil") } - if got, want := err.Error(), `Message: 500 Internal Server Error; body: "important message\n", Locations: [], Extensions: map[code:request_error], Path: []`; got != want { + if got, want := err.Error(), `Message: 500 Internal Server Error, Locations: [], Extensions: map[code:request_error], Path: []`; got != want { t.Errorf("got error: %v, want: %v", got, want) } if q.User.Name != "" { @@ -240,7 +240,7 @@ func TestClient_Query_errorStatusCode(t *testing.T) { t.Errorf("the error type should be graphql.Errors") } gqlErr = err.(graphql.Errors) - if got, want := gqlErr[0].Message, `500 Internal Server Error; body: "important message\n"`; got != want { + if got, want := gqlErr[0].Message, `500 Internal Server Error"`; got != want { t.Errorf("got error: %v, want: %v", got, want) } if got, want := gqlErr[0].Extensions["code"], graphql.ErrRequestError; got != want { From ea35435495dbe099e30c5fdcead2cc141c2cb769 Mon Sep 17 00:00:00 2001 From: Luke Manson Date: Fri, 21 Jun 2024 16:59:33 +0100 Subject: [PATCH 5/5] fix typo --- graphql_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphql_test.go b/graphql_test.go index 87233d1..544ac7d 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -240,7 +240,7 @@ func TestClient_Query_errorStatusCode(t *testing.T) { t.Errorf("the error type should be graphql.Errors") } gqlErr = err.(graphql.Errors) - if got, want := gqlErr[0].Message, `500 Internal Server Error"`; got != want { + if got, want := gqlErr[0].Message, `500 Internal Server Error`; got != want { t.Errorf("got error: %v, want: %v", got, want) } if got, want := gqlErr[0].Extensions["code"], graphql.ErrRequestError; got != want {