Skip to content

Commit

Permalink
Merge pull request #24 from tokopedia/idoej/handle-panic-log
Browse files Browse the repository at this point in the history
feat(panic): log query that causing the panic
  • Loading branch information
yudiharibowo authored Jul 15, 2024
2 parents 6fb9e53 + af203e9 commit eab2ce0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ type Request struct {
SubscribeResolverTimeout time.Duration
}

func (r *Request) handlePanic(ctx context.Context) {
func (r *Request) handlePanic(ctx context.Context, sels ...[]selected.Selection) {
if value := recover(); value != nil {
r.Logger.LogPanic(ctx, value)
var sel selected.Selection
if len(sels) > 0 && len(sels[0]) > 0 {
sel = sels[0][0]
}
err := fmt.Sprintf("%v with query = %v", value, sel)
r.Logger.LogPanic(ctx, err)
r.AddError(r.PanicHandler.MakePanicError(ctx, value))
}
}
Expand All @@ -41,8 +46,9 @@ type extensionser interface {
func (r *Request) Execute(ctx context.Context, s *resolvable.Schema, op *types.OperationDefinition) ([]byte, []*errors.QueryError) {
var out bytes.Buffer
func() {
defer r.handlePanic(ctx)
sels := selected.ApplyOperation(&r.Request, s, op)
var sels []selected.Selection
defer r.handlePanic(ctx, sels)
sels = selected.ApplyOperation(&r.Request, s, op)
r.execSelections(ctx, sels, nil, s, s.Resolver, &out, op.Type == query.Mutation)
}()

Expand Down Expand Up @@ -76,7 +82,7 @@ func (r *Request) execSelections(ctx context.Context, sels []selected.Selection,
for _, f := range fields {
go func(f *fieldToExec) {
defer wg.Done()
defer r.handlePanic(ctx)
defer r.handlePanic(ctx, sels)
f.out = new(bytes.Buffer)
execFieldSelection(ctx, r, s, f, &pathSegment{path, f.field.Alias}, true)
}(f)
Expand Down Expand Up @@ -328,7 +334,7 @@ func (r *Request) execList(ctx context.Context, sels []selected.Selection, typ *
sem <- struct{}{}
go func(i int) {
defer func() { <-sem }()
defer r.handlePanic(ctx)
defer r.handlePanic(ctx, sels)
r.execSelectionSet(ctx, sels, typ.OfType, &pathSegment{path, i}, s, resolver.Index(i), &entryouts[i])
}(i)
}
Expand Down

0 comments on commit eab2ce0

Please sign in to comment.