Skip to content

Commit

Permalink
add option to omit panic handlers during development (#3114)
Browse files Browse the repository at this point in the history
see docs for motivation
  • Loading branch information
mpldr authored Jun 1, 2024
1 parent 1a7c609 commit e07134a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Config struct {
OmitGQLGenVersionInFileNotice bool `yaml:"omit_gqlgen_version_in_file_notice,omitempty"`
OmitRootModels bool `yaml:"omit_root_models,omitempty"`
OmitResolverFields bool `yaml:"omit_resolver_fields,omitempty"`
OmitPanicHandler bool `yaml:"omit_panic_handler,omitempty"`
StructFieldsAlwaysPointers bool `yaml:"struct_fields_always_pointers,omitempty"`
ReturnPointersInUmarshalInput bool `yaml:"return_pointers_in_unmarshalinput,omitempty"`
ResolversAlwaysReturnPointers bool `yaml:"resolvers_always_return_pointers,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions codegen/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ func (ec *executionContext) _{{$object.Name}}_{{$field.Name}}(ctx context.Contex
return {{ $null }}
}
ctx = graphql.WithFieldContext(ctx, fc)
{{- if not $.Config.OmitPanicHandler }}
defer func () {
if r := recover(); r != nil {
ec.Error(ctx, ec.Recover(ctx, r))
ret = {{ $null }}
}
}()
{{- end }}
{{- if $field.TypeReference.IsRoot }}
{{- if $field.TypeReference.IsPtr }}
res := &{{ $field.TypeReference.Elem.GO | ref }}{}
Expand Down Expand Up @@ -95,12 +97,14 @@ func (ec *executionContext) {{ $field.FieldContextFunc }}({{ if not $field.Args
},
}
{{- if $field.Args }}
{{- if not $.Config.OmitPanicHandler }}
defer func () {
if r := recover(); r != nil {
err = ec.Recover(ctx, r)
ec.Error(ctx, err)
}
}()
{{- end }}
ctx = graphql.WithFieldContext(ctx, fc)
if fc.Args, err = ec.{{ $field.ArgsFunc }}(ctx, field.ArgumentMap(ec.Variables)); err != nil {
ec.Error(ctx, err)
Expand Down
2 changes: 2 additions & 0 deletions codegen/object.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ func (ec *executionContext) _{{$object.Name}}(ctx context.Context, sel ast.Selec
field := field

innerFunc := func(ctx context.Context, {{ if $field.TypeReference.GQL.NonNull }}fs{{ else }}_{{ end }} *graphql.FieldSet) (res graphql.Marshaler) {
{{- if not $.Config.OmitPanicHandler }}
defer func() {
if r := recover(); r != nil {
ec.Error(ctx, ec.Recover(ctx, r))
}
}()
{{- end }}
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
2 changes: 2 additions & 0 deletions codegen/type.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,14 @@
}
ctx := graphql.WithFieldContext(ctx, fc)
f := func(i int) {
{{- if not $.Config.OmitPanicHandler }}
defer func() {
if r := recover(); r != nil {
ec.Error(ctx, ec.Recover(ctx, r))
ret = nil
}
}()
{{- end }}
if !isLen1 {
defer wg.Done()
}
Expand Down
11 changes: 9 additions & 2 deletions docs/content/reference/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var errSomethingWrong = errors.New("some validation failed")
// DoThingsReturnMultipleErrors collect errors and returns it if any.
func (r Query) DoThingsReturnMultipleErrors(ctx context.Context) (bool, error) {
errList := gqlerror.List{}

// Add existing error
errList = append(errList, gqlerror.Wrap(errSomethingWrong))

Expand All @@ -95,7 +95,7 @@ func (r Query) DoThingsReturnMultipleErrors(ctx context.Context) (bool, error) {
"code": "10-4",
},
})

return false, errList
}
```
Expand Down Expand Up @@ -172,3 +172,10 @@ server.SetRecoverFunc(func(ctx context.Context, err interface{}) error {
})
```

While these handlers are useful in production to make sure the program does not crash, even if a user finds an issue that causes a crash-condition. During development, it can sometimes be more useful to properly crash, potentially generating a coredump to [enable further debugging](https://go.dev/wiki/CoreDumpDebugging).

To allow your program to crash on a panic, add this to your config file:

```yaml
omit_panic_handler: true
```

0 comments on commit e07134a

Please sign in to comment.