Skip to content

Commit 5719637

Browse files
author
abhif22
committed
Refactored for minimal changes
1 parent 9a09b04 commit 5719637

5 files changed

Lines changed: 20 additions & 19 deletions

File tree

graphql.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ func (s *Schema) exec(ctx context.Context, queryString string, operationName str
223223
varTypes[v.Name.Name] = introspection.WrapType(t)
224224
}
225225
traceCtx, finish := s.tracer.TraceQuery(ctx, queryString, operationName, variables, varTypes)
226-
queryInfo := fmt.Sprintf("Query: %s\nVariables: %+v\n\n", queryString, variables)
227-
data, errs := r.Execute(traceCtx, res, op, queryInfo)
226+
r.QInfo = fmt.Sprintf("Query: %s\nVariables: %+v\n", queryString, variables)
227+
data, errs := r.Execute(traceCtx, res, op)
228228
finish(errs)
229229

230230
return &Response{

internal/exec/exec.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,28 @@ type Request struct {
2424
Limiter chan struct{}
2525
Tracer trace.Tracer
2626
Logger log.Logger
27+
QInfo string
2728
}
2829

29-
func (r *Request) handlePanic(ctx context.Context, queryString string) {
30+
func (r *Request) handlePanic(ctx context.Context) {
3031
if value := recover(); value != nil {
31-
r.Logger.LogPanic(ctx, value)
32-
r.AddError(makePanicError(value, queryString))
32+
r.Logger.LogPanic(ctx, value, r.QInfo)
33+
r.AddError(makePanicError(value))
3334
}
3435
}
3536

3637
type extensionser interface {
3738
Extensions() map[string]interface{}
3839
}
3940

40-
func makePanicError(value interface{}, info string) *errors.QueryError {
41-
return errors.Errorf("graphql: panic occurred: %v\n%s\n\n", value, info)
41+
func makePanicError(value interface{}) *errors.QueryError {
42+
return errors.Errorf("graphql: panic occurred: %v", value)
4243
}
4344

44-
func (r *Request) Execute(ctx context.Context, s *resolvable.Schema, op *query.Operation, queryInfo string) ([]byte, []*errors.QueryError) {
45+
func (r *Request) Execute(ctx context.Context, s *resolvable.Schema, op *query.Operation) ([]byte, []*errors.QueryError) {
4546
var out bytes.Buffer
4647
func() {
47-
defer r.handlePanic(ctx, queryInfo)
48+
defer r.handlePanic(ctx)
4849
sels := selected.ApplyOperation(&r.Request, s, op)
4950
r.execSelections(ctx, sels, nil, s, s.Resolver, &out, op.Type == query.Mutation)
5051
}()
@@ -79,7 +80,7 @@ func (r *Request) execSelections(ctx context.Context, sels []selected.Selection,
7980
for _, f := range fields {
8081
go func(f *fieldToExec) {
8182
defer wg.Done()
82-
defer r.handlePanic(ctx, "")
83+
defer r.handlePanic(ctx)
8384
f.out = new(bytes.Buffer)
8485
execFieldSelection(ctx, r, s, f, &pathSegment{path, f.field.Alias}, true)
8586
}(f)
@@ -177,8 +178,8 @@ func execFieldSelection(ctx context.Context, r *Request, s *resolvable.Schema, f
177178
err = func() (err *errors.QueryError) {
178179
defer func() {
179180
if panicValue := recover(); panicValue != nil {
180-
r.Logger.LogPanic(ctx, panicValue)
181-
err = makePanicError(panicValue, "")
181+
r.Logger.LogPanic(ctx, panicValue, r.QInfo)
182+
err = makePanicError(panicValue)
182183
err.Path = path.toSlice()
183184
}
184185
}()
@@ -337,7 +338,7 @@ func (r *Request) execList(ctx context.Context, sels []selected.Selection, typ *
337338
for i := 0; i < l; i++ {
338339
go func(i int) {
339340
defer wg.Done()
340-
defer r.handlePanic(ctx, "")
341+
defer r.handlePanic(ctx)
341342
r.execSelectionSet(ctx, sels, typ.OfType, &pathSegment{path, i}, s, resolver.Index(i), &entryouts[i])
342343
}(i)
343344
}

internal/exec/subscribe.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (r *Request) Subscribe(ctx context.Context, s *resolvable.Schema, op *query
2525
var f *fieldToExec
2626
var err *errors.QueryError
2727
func() {
28-
defer r.handlePanic(ctx, "")
28+
defer r.handlePanic(ctx)
2929

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

118118
// resolve response
119119
func() {
120-
defer subR.handlePanic(subCtx, "")
120+
defer subR.handlePanic(subCtx)
121121

122122
var buf bytes.Buffer
123123
subR.execSelectionSet(subCtx, f.sels, f.field.Type, &pathSegment{nil, f.field.Alias}, s, resp, &buf)

log/log.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import (
88

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

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

1717
// LogPanic is used to log recovered panic values that occur during query execution
18-
func (l *DefaultLogger) LogPanic(_ context.Context, value interface{}) {
18+
func (l *DefaultLogger) LogPanic(_ context.Context, value interface{}, info string) {
1919
const size = 64 << 10
2020
buf := make([]byte, size)
2121
buf = buf[:runtime.Stack(buf, false)]
22-
log.Printf("graphql: panic occurred: %v\n%s", value, buf)
22+
log.Printf("graphql: panic occurred: %v\n%s\n\n%s", value, buf, info)
2323
}

subscriptions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (s *Schema) subscribe(ctx context.Context, queryString string, operationNam
6565
}
6666

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

0 commit comments

Comments
 (0)