Skip to content

Commit

Permalink
Refactored for minimal changes
Browse files Browse the repository at this point in the history
  • Loading branch information
abhif22 committed Apr 7, 2020
1 parent 7210d03 commit 0cc1057
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
4 changes: 2 additions & 2 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +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)
queryInfo := fmt.Sprintf("Query: %s\nVariables: %+v\n\n", queryString, variables)
data, errs := r.Execute(traceCtx, res, op, queryInfo)
r.QInfo = fmt.Sprintf("Query: %s\nVariables: %+v\n", queryString, variables)
data, errs := r.Execute(traceCtx, res, op)
finish(errs)

return &Response{
Expand Down
23 changes: 12 additions & 11 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,28 @@ type Request struct {
Limiter chan struct{}
Tracer trace.Tracer
Logger log.Logger
QInfo string
}

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

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

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

func (r *Request) Execute(ctx context.Context, s *resolvable.Schema, op *query.Operation, queryInfo string) ([]byte, []*errors.QueryError) {
func (r *Request) Execute(ctx context.Context, s *resolvable.Schema, op *query.Operation) ([]byte, []*errors.QueryError) {
var out bytes.Buffer
func() {
defer r.handlePanic(ctx, queryInfo)
defer r.handlePanic(ctx)
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 +80,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 @@ -177,8 +178,8 @@ func execFieldSelection(ctx context.Context, r *Request, s *resolvable.Schema, f
err = func() (err *errors.QueryError) {
defer func() {
if panicValue := recover(); panicValue != nil {
r.Logger.LogPanic(ctx, panicValue)
err = makePanicError(panicValue, "")
r.Logger.LogPanic(ctx, panicValue, r.QInfo)
err = makePanicError(panicValue)
err.Path = path.toSlice()
}
}()
Expand Down Expand Up @@ -324,7 +325,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
6 changes: 3 additions & 3 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (

// Logger is the interface used to log panics that occur during query execution. It is settable via graphql.ParseSchema
type Logger interface {
LogPanic(ctx context.Context, value interface{})
LogPanic(ctx context.Context, value interface{}, info string)
}

// DefaultLogger is the default logger used to log panics that occur during query execution
type DefaultLogger struct{}

// LogPanic is used to log recovered panic values that occur during query execution
func (l *DefaultLogger) LogPanic(_ context.Context, value interface{}) {
func (l *DefaultLogger) LogPanic(_ context.Context, value interface{}, info string) {
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
log.Printf("graphql: panic occurred: %v\n%s", value, buf)
log.Printf("graphql: panic occurred: %v\n%s\n\n%s", value, buf, info)
}
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 0cc1057

Please sign in to comment.