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

Recover from panics in unlikly places #554

Merged
merged 1 commit into from
Feb 18, 2019
Merged
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
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