Skip to content

Commit

Permalink
Merge pull request #1013 from 99designs/feat-error-dispatch
Browse files Browse the repository at this point in the history
propagate errors to response context in DispatchError
  • Loading branch information
vvakame authored Feb 6, 2020
2 parents 0fe1af8 + 652aa2f commit afa9a15
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions graphql/handler/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ func (e executor) CreateOperationContext(ctx context.Context, params *graphql.Ra

func (e executor) DispatchError(ctx context.Context, list gqlerror.List) *graphql.Response {
ctx = graphql.WithResponseContext(ctx, e.server.errorPresenter, e.server.recoverFunc)
for _, gErr := range list {
graphql.AddError(ctx, gErr)
}

resp := e.responseMiddleware(ctx, func(ctx context.Context) *graphql.Response {
resp := &graphql.Response{
Errors: list,
Expand Down
17 changes: 17 additions & 0 deletions graphql/handler/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/ast"
"github.com/vektah/gqlparser/gqlerror"
"github.com/vektah/gqlparser/parser"
)

Expand Down Expand Up @@ -92,6 +93,22 @@ func TestServer(t *testing.T) {
assert.Equal(t, []string{"first", "second"}, calls)
})

t.Run("get query parse error in AroundResponses", func(t *testing.T) {
var errors1 gqlerror.List
var errors2 gqlerror.List
srv.AroundResponses(func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
resp := next(ctx)
errors1 = graphql.GetErrors(ctx)
errors2 = resp.Errors
return resp
})

resp := get(srv, "/foo?query=invalid")
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
assert.Equal(t, 1, len(errors1))
assert.Equal(t, 1, len(errors2))
})

t.Run("query caching", func(t *testing.T) {
ctx := context.Background()
cache := &graphql.MapCache{}
Expand Down

0 comments on commit afa9a15

Please sign in to comment.