diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7603b46..9da8609 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,7 +13,7 @@ on: jobs: test-go: name: Run Go lint and unit tests - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest permissions: pull-requests: write # Required: allow read access to the content for analysis. @@ -47,7 +47,7 @@ jobs: cd ./example/hasura docker-compose up -d - name: Lint - uses: golangci/golangci-lint-action@v4 + uses: golangci/golangci-lint-action@v6 with: version: latest only-new-issues: true diff --git a/go.mod b/go.mod index 6910049..03937b1 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.20 require ( github.com/google/uuid v1.6.0 - nhooyr.io/websocket v1.8.10 + nhooyr.io/websocket v1.8.11 ) diff --git a/go.sum b/go.sum index b14acc6..50b33e2 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,4 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q= -nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= +nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0= +nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= diff --git a/graphql.go b/graphql.go index 706092d..d72264f 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)) + b, _ := io.ReadAll(resp.Body) + err := newError(ErrRequestError, NetworkError{ + statusCode: resp.StatusCode, + body: string(b), + }) if c.debug { err = err.withRequest(request, reqReader) @@ -386,6 +389,23 @@ func newError(code string, err error) Error { } } +type NetworkError struct { + body string + statusCode int +} + +func (e NetworkError) Error() string { + return fmt.Sprintf("%d %s", e.statusCode, http.StatusText(e.statusCode)) +} + +func (e NetworkError) Body() string { + return e.body +} + +func (e NetworkError) StatusCode() int { + return e.statusCode +} + func (e Error) withRequest(req *http.Request, bodyReader io.Reader) Error { internal := e.getInternalExtension() bodyBytes, err := io.ReadAll(bodyReader) diff --git a/graphql_test.go b/graphql_test.go index 224e926..544ac7d 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 {