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

graphql.ErrorOnPath discards error wrapping around *gqlerror.Error #2286

Closed
andrejgou opened this issue Jul 11, 2022 · 2 comments · Fixed by #2288
Closed

graphql.ErrorOnPath discards error wrapping around *gqlerror.Error #2286

andrejgou opened this issue Jul 11, 2022 · 2 comments · Fixed by #2288

Comments

@andrejgou
Copy link

What happened?

Any errors wrapped around an instance of *gqlerror.Error will be discarded when graphql.ErrorOnPath is called because of this block:

gqlgen/graphql/error.go

Lines 25 to 29 in fb13091

if errors.As(err, &gqlErr) {
if gqlErr.Path == nil {
gqlErr.Path = GetPath(ctx)
}
return gqlErr

Our application uses a custom error presenter to retrieve and log supplemental information annotated on errors in application code or through GraphQL directives using error wrappers, e.g. errors.WithMessage
https://pkg.go.dev/github.com/pkg/errors#WithMessage

Because the error passed to the error presenter is first processed with ErrorOnPath

presentedError := c.errorPresenter(ctx, ErrorOnPath(ctx, err))

we will lose any annotated information wrapped around the original *gqlerror.Error since the error wrappers have been discarded.

See a minimal example to reproduce in a golang playground:
https://go.dev/play/p/TzCt0-D-LRU

err := &gqlerror.Errorf("innermost")
// error annotation in application code or directive
wrappedErr := errors.WithMessage(err, "wrapped message")
// called on input to errorPresenter
strippedErr := graphql.ErrorOnPath(context.Background(), wrappedErr)

fmt.Printf("expect:\t\"%v\"\n", wrappedErr.Error())
fmt.Printf("actual:\t\"%v\"\n", strippedErr.Error())

yields

expect:	"wrapped message: input: innermost"
actual:	"input: innermost"

What did you expect?

The above code snippet should instead output

expect:	"wrapped message: input: innermost"
actual:	"wrapped message: input: innermost"

Minimal graphql.schema and models to reproduce

No schema/model needed. See reproduction:
https://go.dev/play/p/TzCt0-D-LRU

versions

  • go run github.com/99designs/gqlgen version? Any version at 0.13.0 or above since it includes this commit
  • go version? Any go version
@frederikhors
Copy link
Collaborator

Can you PR a fix?

@giautm
Copy link
Contributor

giautm commented Jul 12, 2022

How about this fix? #2288

Tested at: https://go.dev/play/p/wzulxsiB1PL

StevenACoffman pushed a commit that referenced this issue Jul 12, 2022
* fix: return the original error

close #2286

* Update error.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants