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

refactored execFieldSelection to return multiple errors on the same path if err instance of interface { Unwrap() []error } #625

Closed
wants to merge 1 commit 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
48 changes: 32 additions & 16 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,55 +201,71 @@ func execFieldSelection(ctx context.Context, r *Request, s *resolvable.Schema, f
}

var result reflect.Value
var err *errors.QueryError
var errs []*errors.QueryError

traceCtx, finish := r.Tracer.TraceField(ctx, f.field.TraceLabel, f.field.TypeName, f.field.Name, !f.field.Async, f.field.Args)
defer func() {
finish(err)
if len(errs) > 0 {
finish(errs[0]) // unfortunately the signature of finish is not flexible enough to allow us to pass multiple errors
return
}
finish(nil)
}()

err = func() (err *errors.QueryError) {
newError := func(resolverErr error) *errors.QueryError {
err := errors.Errorf("%s", resolverErr)
err.Path = path.toSlice()
err.ResolverError = resolverErr
if ex, ok := resolverErr.(extensionser); ok {
err.Extensions = ex.Extensions()
}
return err
}

func() {
defer func() {
if panicValue := recover(); panicValue != nil {
r.Logger.LogPanic(ctx, panicValue)
err = r.PanicHandler.MakePanicError(ctx, panicValue)
err := r.PanicHandler.MakePanicError(ctx, panicValue)
err.Path = path.toSlice()
errs = append(errs, err)
}
}()

if f.field.FixedResult.IsValid() {
result = f.field.FixedResult
return nil
return
}

if err := traceCtx.Err(); err != nil {
return errors.Errorf("%s", err) // don't execute any more resolvers if context got cancelled
errs = append(errs, errors.Errorf("%s", err))
return
}

res, resolverErr := f.resolve(ctx)
if resolverErr != nil {
err := errors.Errorf("%s", resolverErr)
err.Path = path.toSlice()
err.ResolverError = resolverErr
if ex, ok := resolverErr.(extensionser); ok {
err.Extensions = ex.Extensions()
switch v := resolverErr.(type) {
case interface{ Unwrap() []error }:
for _, err := range v.Unwrap() {
errs = append(errs, newError(err))
}
default:
errs = append(errs, newError(resolverErr))
}
return err
return
}

result = reflect.ValueOf(res)

return nil
}()

if applyLimiter {
<-r.Limiter
}

if err != nil {
if len(errs) > 0 {
// If an error occurred while resolving a field, it should be treated as though the field
// returned null, and an error must be added to the "errors" list in the response.
r.AddError(err)
r.AddError(errs...)
f.out.WriteString("null")
return
}
Expand Down
4 changes: 2 additions & 2 deletions internal/exec/selected/selected.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ type Request struct {
AllowIntrospection bool
}

func (r *Request) AddError(err *errors.QueryError) {
func (r *Request) AddError(errs ...*errors.QueryError) {
r.Mu.Lock()
r.Errs = append(r.Errs, err)
r.Errs = append(r.Errs, errs...)
r.Mu.Unlock()
}

Expand Down
Loading