Skip to content

Commit

Permalink
adding error interface for better separation of library errors and cl…
Browse files Browse the repository at this point in the history
…ient errors
  • Loading branch information
Ajay Kumar committed Sep 28, 2018
1 parent b09572f commit 02135ef
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
8 changes: 4 additions & 4 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"fmt"
)

type GraphQLError interface {
PrepareExtErr() *QueryError
}

type QueryError struct {
Message string `json:"message"`
Locations []Location `json:"locations,omitempty"`
Expand Down Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions internal/exec/resolvable/resolvable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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))
}
}

Expand Down

0 comments on commit 02135ef

Please sign in to comment.