diff --git a/errors/errors.go b/errors/errors.go index fc2ad5c3..a554ddac 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -4,6 +4,10 @@ import ( "fmt" ) +type GraphQLError interface { + PrepareExtErr() *QueryError +} + type QueryError struct { Message string `json:"message"` Locations []Location `json:"locations,omitempty"` @@ -78,7 +82,3 @@ func (err *QueryError) AddErrTimestamp(errTime string) *QueryError { err.Extensions.Timestamp = errTime return err } - -func PrepareExtErr(msg string) *QueryError { - return Errorf("%s", msg) -} diff --git a/internal/exec/exec.go b/internal/exec/exec.go index 0e489cf1..9453ccca 100644 --- a/internal/exec/exec.go +++ b/internal/exec/exec.go @@ -184,8 +184,9 @@ func execFieldSelection(ctx context.Context, r *Request, f *fieldToExec, path *p callOut := f.resolver.Method(f.field.MethodIndex).Call(in) result = callOut[0] if f.field.HasError && !callOut[1].IsNil() { - extnErr, ok := callOut[1].Interface().(*errors.QueryError) + graphQLErr, ok := callOut[1].Interface().(errors.GraphQLError) if ok { + extnErr := graphQLErr.PrepareExtErr() extnErr.Path = path.toSlice() extnErr.ResolverError = errlib.New(extnErr.Message) return extnErr diff --git a/internal/exec/resolvable/resolvable.go b/internal/exec/resolvable/resolvable.go index 3739420e..c739de60 100644 --- a/internal/exec/resolvable/resolvable.go +++ b/internal/exec/resolvable/resolvable.go @@ -253,7 +253,7 @@ func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, p var contextType = reflect.TypeOf((*context.Context)(nil)).Elem() var errorType = reflect.TypeOf((*error)(nil)).Elem() -var extnErrorType = reflect.TypeOf((**errors.QueryError)(nil)).Elem() +var extnErrorInterfaceType = reflect.TypeOf((*errors.GraphQLError)(nil)).Elem() func (b *execBuilder) makeFieldExec(typeName string, f *schema.Field, m reflect.Method, methodIndex int, methodHasReceiver bool) (*Field, error) { in := make([]reflect.Type, m.Type.NumIn()) @@ -292,8 +292,8 @@ func (b *execBuilder) makeFieldExec(typeName string, f *schema.Field, m reflect. hasError := m.Type.NumOut() == 2 if hasError { - if m.Type.Out(1) != errorType && m.Type.Out(1) != extnErrorType { - return nil, fmt.Errorf(`must have "error" or "*errors.QueryError" as its second return value`) + if m.Type.Out(1) != errorType && !m.Type.Out(1).Implements(extnErrorInterfaceType) { + return nil, fmt.Errorf(`must have "error" or implements errors.GraphQLError interface as its second return value but the type is %v`, m.Type.Out(1)) } }