Skip to content

Commit

Permalink
Merge branch 'master' into graphql-operation-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Jun 25, 2024
2 parents 806f248 + fa97047 commit b1f94ac
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
24 changes: 22 additions & 2 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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 {
Expand Down

0 comments on commit b1f94ac

Please sign in to comment.