Skip to content

Commit

Permalink
Merge pull request #978 from 99designs/pluggable-error-code
Browse files Browse the repository at this point in the history
Allow customizing http and websocket status codes for errors
  • Loading branch information
vektah committed Jan 15, 2020
2 parents 28c032d + 7f6f166 commit ae79e75
Show file tree
Hide file tree
Showing 30 changed files with 5,758 additions and 4,941 deletions.
10 changes: 9 additions & 1 deletion api/generate.go
Expand Up @@ -95,7 +95,15 @@ func validate(cfg *config.Config) error {
if cfg.Resolver.IsDefined() {
roots = append(roots, cfg.Resolver.ImportPath())
}
_, err := packages.Load(&packages.Config{Mode: packages.LoadTypes | packages.LoadSyntax}, roots...)
_, err := packages.Load(&packages.Config{
Mode: packages.NeedName |
packages.NeedFiles |
packages.NeedCompiledGoFiles |
packages.NeedImports |
packages.NeedTypes |
packages.NeedTypesSizes |
packages.NeedSyntax |
packages.NeedTypesInfo}, roots...)
if err != nil {
return errors.Wrap(err, "validation failed")
}
Expand Down
11 changes: 10 additions & 1 deletion codegen/config/binder.go
Expand Up @@ -24,7 +24,16 @@ type Binder struct {
}

func (c *Config) NewBinder(s *ast.Schema) (*Binder, error) {
pkgs, err := packages.Load(&packages.Config{Mode: packages.LoadTypes | packages.LoadSyntax}, c.Models.ReferencedPackages()...)
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName |
packages.NeedFiles |
packages.NeedCompiledGoFiles |
packages.NeedImports |
packages.NeedTypes |
packages.NeedTypesSizes |
packages.NeedSyntax |
packages.NeedTypesInfo,
}, c.Models.ReferencedPackages()...)
if err != nil {
return nil, err
}
Expand Down
10 changes: 9 additions & 1 deletion codegen/config/config.go
Expand Up @@ -400,7 +400,15 @@ func (c *Config) Autobind(s *ast.Schema) error {
if len(c.AutoBind) == 0 {
return nil
}
ps, err := packages.Load(&packages.Config{Mode: packages.LoadTypes}, c.AutoBind...)

ps, err := packages.Load(&packages.Config{
Mode: packages.NeedName |
packages.NeedFiles |
packages.NeedCompiledGoFiles |
packages.NeedImports |
packages.NeedTypes |
packages.NeedTypesSizes,
}, c.AutoBind...)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions codegen/testserver/complexity_test.go
Expand Up @@ -74,7 +74,7 @@ func TestComplexityFuncs(t *testing.T) {
}
err := c.Post(`query { overlapping { oneFoo, twoFoo, oldFoo, newFoo, new_foo } }`, &resp)

require.EqualError(t, err, `http 422: {"errors":[{"message":"operation has complexity 2012, which exceeds the limit of 10"}],"data":null}`)
require.EqualError(t, err, `[{"message":"operation has complexity 2012, which exceeds the limit of 10","extensions":{"code":"COMPLEXITY_LIMIT_EXCEEDED"}}]`)
require.False(t, ran)
})

Expand Down Expand Up @@ -113,7 +113,7 @@ func TestComplexityFuncs(t *testing.T) {
c: overlapping { newFoo },
}`, &resp)

require.EqualError(t, err, `http 422: {"errors":[{"message":"operation has complexity 18, which exceeds the limit of 10"}],"data":null}`)
require.EqualError(t, err, `[{"message":"operation has complexity 18, which exceeds the limit of 10","extensions":{"code":"COMPLEXITY_LIMIT_EXCEEDED"}}]`)
require.False(t, ran)
})
}
4 changes: 2 additions & 2 deletions codegen/testserver/enums_test.go
Expand Up @@ -36,7 +36,7 @@ func TestEnumsResolver(t *testing.T) {
enumInInput(input: {enum: INVALID})
}
`, &resp)
require.EqualError(t, err, `http 422: {"errors":[{"message":"Expected type EnumTest!, found INVALID.","locations":[{"line":2,"column":30}]}],"data":null}`)
require.EqualError(t, err, `http 422: {"errors":[{"message":"Expected type EnumTest!, found INVALID.","locations":[{"line":2,"column":30}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`)
})

t.Run("input with invalid enum value via vars", func(t *testing.T) {
Expand All @@ -47,6 +47,6 @@ func TestEnumsResolver(t *testing.T) {
enumInInput(input: $input)
}
`, &resp, client.Var("input", map[string]interface{}{"enum": "INVALID"}))
require.EqualError(t, err, `http 422: {"errors":[{"message":"INVALID is not a valid EnumTest","path":["variable","input","enum"]}],"data":null}`)
require.EqualError(t, err, `http 422: {"errors":[{"message":"INVALID is not a valid EnumTest","path":["variable","input","enum"],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`)
})
}
2 changes: 1 addition & 1 deletion codegen/testserver/input_test.go
Expand Up @@ -25,7 +25,7 @@ func TestInput(t *testing.T) {

err := c.Post(`query { inputSlice(arg: ["ok", 1, 2, "ok"]) }`, &resp)

require.EqualError(t, err, `http 422: {"errors":[{"message":"Expected type String!, found 1.","locations":[{"line":1,"column":32}]},{"message":"Expected type String!, found 2.","locations":[{"line":1,"column":35}]}],"data":null}`)
require.EqualError(t, err, `http 422: {"errors":[{"message":"Expected type String!, found 1.","locations":[{"line":1,"column":32}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}},{"message":"Expected type String!, found 2.","locations":[{"line":1,"column":35}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`)
require.Nil(t, resp.DirectiveArg)
})
}
2 changes: 1 addition & 1 deletion example/starwars/starwars_test.go
Expand Up @@ -214,7 +214,7 @@ func TestStarwars(t *testing.T) {
}
}`, &resp, client.Var("episode", "INVALID"))

require.EqualError(t, err, `http 422: {"errors":[{"message":"INVALID is not a valid Episode","path":["variable","episode"]}],"data":null}`)
require.EqualError(t, err, `http 422: {"errors":[{"message":"INVALID is not a valid Episode","path":["variable","episode"],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`)
})

t.Run("introspection", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -11,7 +11,7 @@ require (
github.com/gorilla/websocket v1.2.0
github.com/hashicorp/golang-lru v0.5.0
github.com/kr/pretty v0.1.0 // indirect
github.com/matryer/moq v0.0.0-20200106131100-75d0ddfc0007 // indirect
github.com/matryer/moq v0.0.0-20200106131100-75d0ddfc0007
github.com/mitchellh/mapstructure v0.0.0-20180203102830-a4e142e9c047
github.com/opentracing/basictracer-go v1.0.0 // indirect
github.com/opentracing/opentracing-go v1.0.2
Expand All @@ -23,7 +23,7 @@ require (
github.com/urfave/cli v1.20.0
github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e
github.com/vektah/gqlparser v1.2.1
golang.org/x/tools v0.0.0-20190515012406-7d7faa4812bd
golang.org/x/tools v0.0.0-20200114235610-7ae403b6b589
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.2.2
sourcegraph.com/sourcegraph/appdash v0.0.0-20180110180208-2cc67fd64755
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Expand Up @@ -55,15 +55,24 @@ github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e/go.mod h1:/HUd
github.com/vektah/gqlparser v1.2.1 h1:C+L7Go/eUbN0w6Y0kaiq2W6p2wN5j8wU82EdDXxDivc=
github.com/vektah/gqlparser v1.2.1/go.mod h1:bkVf0FX+Stjg/MHnm8mEyubuaArhNEqfQhF+OTiAL74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190125232054-d66bd3c5d5a6 h1:iZgcI2DDp6zW5v9Z/5+f0NuqoxNdmzg4hivjk2WLXpY=
golang.org/x/tools v0.0.0-20190125232054-d66bd3c5d5a6/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190515012406-7d7faa4812bd h1:oMEQDWVXVNpceQoVd1JN3CQ7LYJJzs5qWqZIUcxXHHw=
golang.org/x/tools v0.0.0-20190515012406-7d7faa4812bd/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20200114235610-7ae403b6b589 h1:rjUrONFu4kLchcZTfp3/96bR8bW8dIa8uz3cR5n0cgM=
golang.org/x/tools v0.0.0-20200114235610-7ae403b6b589/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
Expand Down
49 changes: 49 additions & 0 deletions graphql/errcode/codes.go
@@ -0,0 +1,49 @@
package errcode

import (
"github.com/vektah/gqlparser/gqlerror"
)

const ValidationFailed = "GRAPHQL_VALIDATION_FAILED"
const ParseFailed = "GRAPHQL_PARSE_FAILED"

type ErrorKind int

const (
// issues with graphql (validation, parsing). 422s in http, GQL_ERROR in websocket
KindProtocol ErrorKind = iota
// user errors, 200s in http, GQL_DATA in websocket
KindUser
)

var codeType = map[string]ErrorKind{
ValidationFailed: KindProtocol,
ParseFailed: KindProtocol,
}

// RegisterErrorType should be called by extensions that want to customize the http status codes for errors they return
func RegisterErrorType(code string, kind ErrorKind) {
codeType[code] = kind
}

// Set the error code on a given graphql error extension
func Set(err *gqlerror.Error, value string) {
if err.Extensions == nil {
err.Extensions = map[string]interface{}{}
}

err.Extensions["code"] = value
}

// get the kind of the first non User error, defaults to User if no errors have a custom extension
func GetErrorKind(errs gqlerror.List) ErrorKind {
for _, err := range errs {
if code, ok := err.Extensions["code"].(string); ok {
if kind, ok := codeType[code]; ok && kind != KindUser {
return kind
}
}
}

return KindUser
}
2 changes: 1 addition & 1 deletion graphql/handler/apollotracing/tracer_test.go
Expand Up @@ -60,7 +60,7 @@ func TestApolloTracing_withFail(t *testing.T) {
h.Use(apollotracing.Tracer{})

resp := doRequest(h, "POST", "/graphql", `{"operationName":"A","extensions":{"persistedQuery":{"version":1,"sha256Hash":"338bbc16ac780daf81845339fbf0342061c1e9d2b702c96d3958a13a557083a6"}}}`)
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
assert.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
b := resp.Body.Bytes()
t.Log(string(b))
var respData struct {
Expand Down
6 changes: 6 additions & 0 deletions graphql/handler/executor.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/errcode"
"github.com/vektah/gqlparser/ast"
"github.com/vektah/gqlparser/gqlerror"
"github.com/vektah/gqlparser/parser"
Expand Down Expand Up @@ -147,6 +148,7 @@ func (e executor) CreateOperationContext(ctx context.Context, params *graphql.Ra
var err *gqlerror.Error
rc.Variables, err = validator.VariableValues(e.server.es.Schema(), rc.Operation, params.Variables)
if err != nil {
errcode.Set(err, errcode.ValidationFailed)
return rc, gqlerror.List{err}
}
rc.Stats.Validation.End = graphql.Now()
Expand Down Expand Up @@ -190,13 +192,17 @@ func (e executor) parseQuery(ctx context.Context, stats *graphql.Stats, query st

doc, err := parser.ParseQuery(&ast.Source{Input: query})
if err != nil {
errcode.Set(err, errcode.ParseFailed)
return nil, gqlerror.List{err}
}
stats.Parsing.End = graphql.Now()

stats.Validation.Start = graphql.Now()
listErr := validator.Validate(e.server.es.Schema(), doc)
if len(listErr) != 0 {
for _, e := range listErr {
errcode.Set(e, errcode.ValidationFailed)
}
return nil, listErr
}

Expand Down
7 changes: 6 additions & 1 deletion graphql/handler/extension/apq.go
Expand Up @@ -6,13 +6,16 @@ import (
"encoding/hex"
"fmt"

"github.com/99designs/gqlgen/graphql/errcode"

"github.com/vektah/gqlparser/gqlerror"

"github.com/99designs/gqlgen/graphql"
"github.com/mitchellh/mapstructure"
)

const errPersistedQueryNotFound = "PersistedQueryNotFound"
const errPersistedQueryNotFoundCode = "PERSISTED_QUERY_NOT_FOUND"

// AutomaticPersistedQuery saves client upload by optimistically sending only the hashes of queries, if the server
// does not yet know what the query is for the hash it will respond telling the client to send the query along with the
Expand Down Expand Up @@ -71,7 +74,9 @@ func (a AutomaticPersistedQuery) MutateOperationParameters(ctx context.Context,
// client sent optimistic query hash without query string, get it from the cache
query, ok := a.Cache.Get(extension.Sha256)
if !ok {
return gqlerror.Errorf(errPersistedQueryNotFound)
err := gqlerror.Errorf(errPersistedQueryNotFound)
errcode.Set(err, errPersistedQueryNotFoundCode)
return err
}
rawParams.Query = query.(string)
} else {
Expand Down
7 changes: 6 additions & 1 deletion graphql/handler/extension/complexity.go
Expand Up @@ -6,9 +6,12 @@ import (

"github.com/99designs/gqlgen/complexity"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/errcode"
"github.com/vektah/gqlparser/gqlerror"
)

const errComplexityLimit = "COMPLEXITY_LIMIT_EXCEEDED"

// ComplexityLimit allows you to define a limit on query complexity
//
// If a query is submitted that exceeds the limit, a 422 status code will be returned.
Expand Down Expand Up @@ -66,7 +69,9 @@ func (c ComplexityLimit) MutateOperationContext(ctx context.Context, rc *graphql
})

if complexity > limit {
return gqlerror.Errorf("operation has complexity %d, which exceeds the limit of %d", complexity, limit)
err := gqlerror.Errorf("operation has complexity %d, which exceeds the limit of %d", complexity, limit)
errcode.Set(err, errComplexityLimit)
return err
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions graphql/handler/extension/complexity_test.go
Expand Up @@ -46,8 +46,8 @@ func TestHandlerComplexity(t *testing.T) {
stats = nil
h.SetCalculatedComplexity(4)
resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }"}`)
require.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
require.Equal(t, `{"errors":[{"message":"operation has complexity 4, which exceeds the limit of 2"}],"data":null}`, resp.Body.String())
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"errors":[{"message":"operation has complexity 4, which exceeds the limit of 2","extensions":{"code":"COMPLEXITY_LIMIT_EXCEEDED"}}],"data":null}`, resp.Body.String())

require.Equal(t, 2, stats.ComplexityLimit)
require.Equal(t, 4, stats.Complexity)
Expand Down Expand Up @@ -89,8 +89,8 @@ func TestFixedComplexity(t *testing.T) {
t.Run("above complexity limit", func(t *testing.T) {
h.SetCalculatedComplexity(4)
resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }"}`)
require.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
require.Equal(t, `{"errors":[{"message":"operation has complexity 4, which exceeds the limit of 2"}],"data":null}`, resp.Body.String())
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"errors":[{"message":"operation has complexity 4, which exceeds the limit of 2","extensions":{"code":"COMPLEXITY_LIMIT_EXCEEDED"}}],"data":null}`, resp.Body.String())

require.Equal(t, 2, stats.ComplexityLimit)
require.Equal(t, 4, stats.Complexity)
Expand Down
2 changes: 1 addition & 1 deletion graphql/handler/transport/http_form.go
Expand Up @@ -189,7 +189,7 @@ func (f MultipartForm) Do(w http.ResponseWriter, r *http.Request, exec graphql.G
rc, gerr := exec.CreateOperationContext(r.Context(), &params)
if gerr != nil {
resp := exec.DispatchError(graphql.WithOperationContext(r.Context(), rc), gerr)
w.WriteHeader(http.StatusUnprocessableEntity)
w.WriteHeader(statusFor(gerr))
writeJson(w, resp)
return
}
Expand Down
13 changes: 12 additions & 1 deletion graphql/handler/transport/http_get.go
Expand Up @@ -7,7 +7,9 @@ import (
"strings"

"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/errcode"
"github.com/vektah/gqlparser/ast"
"github.com/vektah/gqlparser/gqlerror"
)

// GET implements the GET side of the default HTTP transport
Expand Down Expand Up @@ -48,7 +50,7 @@ func (h GET) Do(w http.ResponseWriter, r *http.Request, exec graphql.GraphExecut

rc, err := exec.CreateOperationContext(r.Context(), raw)
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
w.WriteHeader(statusFor(err))
resp := exec.DispatchError(graphql.WithOperationContext(r.Context(), rc), err)
writeJson(w, resp)
return
Expand All @@ -69,3 +71,12 @@ func jsonDecode(r io.Reader, val interface{}) error {
dec.UseNumber()
return dec.Decode(val)
}

func statusFor(errs gqlerror.List) int {
switch errcode.GetErrorKind(errs) {
case errcode.KindProtocol:
return http.StatusUnprocessableEntity
default:
return http.StatusOK
}
}
4 changes: 2 additions & 2 deletions graphql/handler/transport/http_get_test.go
Expand Up @@ -28,13 +28,13 @@ func TestGET(t *testing.T) {
t.Run("invalid variable", func(t *testing.T) {
resp := doRequest(h, "GET", `/graphql?query=query($id:Int!){find(id:$id)}&variables={"id":false}`, "")
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
assert.Equal(t, `{"errors":[{"message":"cannot use bool as Int","path":["variable","id"]}],"data":null}`, resp.Body.String())
assert.Equal(t, `{"errors":[{"message":"cannot use bool as Int","path":["variable","id"],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`, resp.Body.String())
})

t.Run("parse failure", func(t *testing.T) {
resp := doRequest(h, "GET", "/graphql?query=!", "")
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
assert.Equal(t, `{"errors":[{"message":"Unexpected !","locations":[{"line":1,"column":1}]}],"data":null}`, resp.Body.String())
assert.Equal(t, `{"errors":[{"message":"Unexpected !","locations":[{"line":1,"column":1}],"extensions":{"code":"GRAPHQL_PARSE_FAILED"}}],"data":null}`, resp.Body.String())
})

t.Run("no mutations", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion graphql/handler/transport/http_post.go
Expand Up @@ -38,7 +38,7 @@ func (h POST) Do(w http.ResponseWriter, r *http.Request, exec graphql.GraphExecu

rc, err := exec.CreateOperationContext(r.Context(), params)
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
w.WriteHeader(statusFor(err))
resp := exec.DispatchError(graphql.WithOperationContext(r.Context(), rc), err)
writeJson(w, resp)
return
Expand Down

0 comments on commit ae79e75

Please sign in to comment.