Skip to content

Commit

Permalink
Merge pull request #7 from getamis/carawang/feature/ws-495
Browse files Browse the repository at this point in the history
graphql: can parse error from graphql server
  • Loading branch information
CaraWang authored Feb 20, 2023
2 parents e27479b + 038f502 commit a815083
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 6 additions & 4 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,17 +311,19 @@ func ImmediatelyCloseReqBody() ClientOption {
// modify the behaviour of the Client.
type ClientOption func(*Client)

type graphErr struct {
Message string
type GraphQLErr struct {
Message string `json:"message"`
Path []interface{} `json:"path,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}

func (e graphErr) Error() string {
func (e GraphQLErr) Error() string {
return "graphql: " + e.Message
}

type graphResponse struct {
Data interface{}
Errors []graphErr
Errors []GraphQLErr
}

// Request is a GraphQL request.
Expand Down
6 changes: 5 additions & 1 deletion graphql_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ func TestDoJSONBadRequestErr(t *testing.T) {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{
"errors": [{
"message": "miscellaneous message as to why the the request was bad"
"message": "miscellaneous message as to why the the request was bad",
"path": ["location"],
"extensions": {"code": "BAD_USER_INPUT", "subCode": 1063}
}]
}`)
}))
Expand All @@ -93,6 +95,8 @@ func TestDoJSONBadRequestErr(t *testing.T) {
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.(GraphQLErr).Path[0], "location")
is.Equal(err.(GraphQLErr).Extensions["code"], "BAD_USER_INPUT")
}

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

0 comments on commit a815083

Please sign in to comment.