Skip to content

Commit

Permalink
Changes for logging query string during panics
Browse files Browse the repository at this point in the history
  • Loading branch information
abhif22 committed Apr 6, 2020
1 parent dae2e13 commit 7210d03
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
3 changes: 2 additions & 1 deletion graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ func (s *Schema) exec(ctx context.Context, queryString string, operationName str
varTypes[v.Name.Name] = introspection.WrapType(t)
}
traceCtx, finish := s.tracer.TraceQuery(ctx, queryString, operationName, variables, varTypes)
data, errs := r.Execute(traceCtx, res, op)
queryInfo := fmt.Sprintf("Query: %s\nVariables: %+v\n\n", queryString, variables)
data, errs := r.Execute(traceCtx, res, op, queryInfo)
finish(errs)

return &Response{
Expand Down
18 changes: 9 additions & 9 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ type Request struct {
Logger log.Logger
}

func (r *Request) handlePanic(ctx context.Context) {
func (r *Request) handlePanic(ctx context.Context, queryString string) {
if value := recover(); value != nil {
r.Logger.LogPanic(ctx, value)
r.AddError(makePanicError(value))
r.AddError(makePanicError(value, queryString))
}
}

type extensionser interface {
Extensions() map[string]interface{}
}

func makePanicError(value interface{}) *errors.QueryError {
return errors.Errorf("graphql: panic occurred: %v", value)
func makePanicError(value interface{}, info string) *errors.QueryError {
return errors.Errorf("graphql: panic occurred: %v\n%s\n\n", value, info)
}

func (r *Request) Execute(ctx context.Context, s *resolvable.Schema, op *query.Operation) ([]byte, []*errors.QueryError) {
func (r *Request) Execute(ctx context.Context, s *resolvable.Schema, op *query.Operation, queryInfo string) ([]byte, []*errors.QueryError) {
var out bytes.Buffer
func() {
defer r.handlePanic(ctx)
defer r.handlePanic(ctx, queryInfo)
sels := selected.ApplyOperation(&r.Request, s, op)
r.execSelections(ctx, sels, nil, s, s.Resolver, &out, op.Type == query.Mutation)
}()
Expand Down Expand Up @@ -79,7 +79,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, "")
f.out = new(bytes.Buffer)
execFieldSelection(ctx, r, s, f, &pathSegment{path, f.field.Alias}, true)
}(f)
Expand Down Expand Up @@ -178,7 +178,7 @@ func execFieldSelection(ctx context.Context, r *Request, s *resolvable.Schema, f
defer func() {
if panicValue := recover(); panicValue != nil {
r.Logger.LogPanic(ctx, panicValue)
err = makePanicError(panicValue)
err = makePanicError(panicValue, "")
err.Path = path.toSlice()
}
}()
Expand Down Expand Up @@ -324,7 +324,7 @@ func (r *Request) execList(ctx context.Context, sels []selected.Selection, typ *
for i := 0; i < l; i++ {
go func(i int) {
defer wg.Done()
defer r.handlePanic(ctx)
defer r.handlePanic(ctx, "")
r.execSelectionSet(ctx, sels, typ.OfType, &pathSegment{path, i}, s, resolver.Index(i), &entryouts[i])
}(i)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/exec/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (r *Request) Subscribe(ctx context.Context, s *resolvable.Schema, op *query
var f *fieldToExec
var err *errors.QueryError
func() {
defer r.handlePanic(ctx)
defer r.handlePanic(ctx, "")

sels := selected.ApplyOperation(&r.Request, s, op)
var fields []*fieldToExec
Expand Down Expand Up @@ -117,7 +117,7 @@ func (r *Request) Subscribe(ctx context.Context, s *resolvable.Schema, op *query

// resolve response
func() {
defer subR.handlePanic(subCtx)
defer subR.handlePanic(subCtx, "")

var buf bytes.Buffer
subR.execSelectionSet(subCtx, f.sels, f.field.Type, &pathSegment{nil, f.field.Alias}, s, resp, &buf)
Expand Down
2 changes: 1 addition & 1 deletion subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *Schema) subscribe(ctx context.Context, queryString string, operationNam
}

if op.Type == query.Query || op.Type == query.Mutation {
data, errs := r.Execute(ctx, res, op)
data, errs := r.Execute(ctx, res, op, "")
return sendAndReturnClosed(&Response{Data: data, Errors: errs})
}

Expand Down

0 comments on commit 7210d03

Please sign in to comment.