Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support returning standard graphQL errors instead of just a message #52

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/machinebox/graphql

go 1.12

require (
github.com/matryer/is v1.2.0
github.com/pkg/errors v0.8.1
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A=
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
20 changes: 15 additions & 5 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,27 @@ func ImmediatelyCloseReqBody() ClientOption {
// modify the behaviour of the Client.
type ClientOption func(*Client)

type graphErr struct {
Message string
// Location represents the location of a error
type Location struct {
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
}

func (e graphErr) Error() string {
return "graphql: " + e.Message
// GraphError represents the standard graphql error described in https://graphql.github.io/graphql-spec/June2018/#sec-Errors
type GraphError struct {
Message string `json:"message"`
Path []interface{} `json:"path,omitempty"`
Locations []Location `json:"locations,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}

func (e GraphError) Error() string {
return fmt.Sprintf("graphql: %s", e.Message)
}

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

// Request is a GraphQL request.
Expand Down
78 changes: 78 additions & 0 deletions graphql_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,81 @@ func TestHeader(t *testing.T) {

is.Equal(resp.Value, "some data")
}

func TestErrors(t *testing.T) {
is := is.New(t)
type errorSuit struct {
response string
statusCode int
expectedMessage string
expected GraphError
}
var suits = []errorSuit{
errorSuit{
response: `{
"errors": [
{
"message": "Name for character with ID 1002 could not be fetched.",
"locations": [ { "line": 6, "column": 7 } ],
"path": [ "hero", "heroFriends", 1, "name" ],
"extensions": {
"code": "CAN_NOT_FETCH_BY_ID",
"timestamp": "Fri Feb 9 14:33:09 UTC 2018"
}
}
]
}`,
statusCode: 404,
expectedMessage: "graphql: Name for character with ID 1002 could not be fetched.",
expected: GraphError{
Message: "Name for character with ID 1002 could not be fetched.",
Locations: []Location{
Location{Line: 6, Column: 7},
},
Path: []interface{}{"hero", "heroFriends", float64(1), "name"},
Extensions: map[string]interface{}{
"code": "CAN_NOT_FETCH_BY_ID",
"timestamp": "Fri Feb 9 14:33:09 UTC 2018",
},
},
},
errorSuit{
response: `{
"errors": [
{
"message": "Server error"
}
]
}`,
statusCode: 500,
expectedMessage: "graphql: Server error",
expected: GraphError{
Message: "Server error",
},
},
}
for _, suit := range suits {
func() {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(suit.statusCode)
_, err := io.WriteString(w, suit.response)
is.NoErr(err)
}))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
client := NewClient(srv.URL)
req := NewRequest("query {}")
var resp struct {
Value string
}
err := client.Run(ctx, req, &resp)
if err != nil {
is.Equal(err.Error(), suit.expectedMessage)
is.Equal(err, suit.expected)
} else {
is.Fail()
}
}()
}
}