Skip to content

Commit

Permalink
Merge pull request #620 from codyleyhan/cl/error
Browse files Browse the repository at this point in the history
Allow user to supply path to gqlerror
  • Loading branch information
vektah authored Mar 14, 2019
2 parents 932322b + 12cf01a commit 26c395b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 19 additions & 0 deletions graphql/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/ast"
"github.com/vektah/gqlparser/gqlerror"
)

func TestRequestContext_GetErrors(t *testing.T) {
Expand All @@ -33,8 +34,21 @@ func TestRequestContext_GetErrors(t *testing.T) {
Parent: root,
Index: &index,
}
userProvidedPath := &ResolverContext{
Parent: child,
Field: CollectedField{
Field: &ast.Field{
Alias: "works",
},
},
}

ctx = WithResolverContext(ctx, child)
c.Error(ctx, errors.New("bar"))
c.Error(ctx, &gqlerror.Error{
Message: "foo3",
Path: append(child.Path(), "works"),
})

specs := []struct {
Name string
Expand All @@ -51,6 +65,11 @@ func TestRequestContext_GetErrors(t *testing.T) {
RCtx: child,
Messages: []string{"bar"},
},
{
Name: "with user provided path",
RCtx: userProvidedPath,
Messages: []string{"foo3"},
},
}

for _, spec := range specs {
Expand Down
4 changes: 3 additions & 1 deletion graphql/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ type ExtendedError interface {

func DefaultErrorPresenter(ctx context.Context, err error) *gqlerror.Error {
if gqlerr, ok := err.(*gqlerror.Error); ok {
gqlerr.Path = GetResolverContext(ctx).Path()
if gqlerr.Path == nil {
gqlerr.Path = GetResolverContext(ctx).Path()
}
return gqlerr
}

Expand Down

0 comments on commit 26c395b

Please sign in to comment.