Skip to content

Commit

Permalink
Recover from panics in unlikly places
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Feb 18, 2019
1 parent 327a1a3 commit 67795c9
Show file tree
Hide file tree
Showing 21 changed files with 1,425 additions and 12 deletions.
2 changes: 1 addition & 1 deletion codegen/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ nextArg:
}

// no matching arg found, abort
return fmt.Errorf("%s is not in schema", param.Name())
return fmt.Errorf("arg %s not in schema", param.Name())
}

field.Args = newArgs
Expand Down
8 changes: 6 additions & 2 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var InterfaceType = types.NewInterfaceType(nil, nil)
func (b *Binder) DefaultUserObject(name string) (types.Type, error) {
models := b.cfg.Models[name].Model
if len(models) == 0 {
return nil, fmt.Errorf(name + " not found")
return nil, fmt.Errorf(name + " not found in typemap")
}

if models[0] == "map[string]interface{}" {
Expand Down Expand Up @@ -346,6 +346,10 @@ func (b *Binder) TypeReference(schemaType *ast.Type, bindTarget types.Type) (ret
}
}()

if len(b.cfg.Models[schemaType.Name()].Model) == 0 {
return nil, fmt.Errorf("%s was not found", schemaType.Name())
}

for _, model := range b.cfg.Models[schemaType.Name()].Model {
if model == "map[string]interface{}" {
if !isMap(bindTarget) {
Expand Down Expand Up @@ -404,7 +408,7 @@ func (b *Binder) TypeReference(schemaType *ast.Type, bindTarget types.Type) (ret
return ref, nil
}

return nil, fmt.Errorf("not found")
return nil, fmt.Errorf("%s has type compatible with %s", schemaType.Name(), bindTarget.String())
}

func (b *Binder) CopyModifiersFromAst(t *ast.Type, usePtr bool, base types.Type) types.Type {
Expand Down
12 changes: 6 additions & 6 deletions codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,14 @@ func (b *builder) buildField(obj *Object, field *ast.FieldDefinition) (*Field, e
}

if err = b.bindField(obj, &f); err != nil {
f.IsResolver = true
log.Println(err.Error())
}

if f.IsResolver && !f.TypeReference.IsPtr() && f.TypeReference.IsStruct() {
f.TypeReference = b.Binder.PointerTo(f.TypeReference)
}

return &f, nil
}

Expand All @@ -77,10 +82,6 @@ func (b *builder) bindField(obj *Object, f *Field) error {
}
f.TypeReference = tr
}

if f.IsResolver && !f.TypeReference.IsPtr() && f.TypeReference.IsStruct() {
f.TypeReference = b.Binder.PointerTo(f.TypeReference)
}
}()

switch {
Expand Down Expand Up @@ -115,8 +116,6 @@ func (b *builder) bindField(obj *Object, f *Field) error {

switch target := target.(type) {
case nil:
f.IsResolver = true

objPos := b.Binder.TypePosition(obj.Type)
return fmt.Errorf(
"%s:%d adding resolver method for %s.%s, nothing matched",
Expand All @@ -125,6 +124,7 @@ func (b *builder) bindField(obj *Object, f *Field) error {
obj.Name,
f.Name,
)

case *types.Func:
sig := target.Type().(*types.Signature)
if sig.Results().Len() == 1 {
Expand Down
5 changes: 5 additions & 0 deletions codegen/object.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func (ec *executionContext) _{{$object.Name}}(ctx context.Context, sel ast.Selec
{{- if $field.IsConcurrent }}
field := field
out.Concurrently(i, func() (res graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
ec.Error(ctx, ec.Recover(ctx, r))
}
}()
res = ec._{{$object.Name}}_{{$field.Name}}(ctx, field{{if not $object.Root}}, obj{{end}})
{{- if $field.TypeReference.GQL.NonNull }}
if res == graphql.Null {
Expand Down
Loading

0 comments on commit 67795c9

Please sign in to comment.