Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove error wrapping changes #11

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Schema struct {
logger log.Logger
useStringDescriptions bool
disableIntrospection bool
isPanicLogEnabled bool
}

// SchemaOpt is an option to pass to ParseSchema or MustParseSchema.
Expand Down Expand Up @@ -150,6 +151,19 @@ func (s *Schema) Validate(queryString string, variables map[string]interface{})
return false, validation.Validate(s.schema, doc, variables, s.maxDepth)
}

//EnablePanicLogging enables query info logging if panic occurs
func (s *Schema) EnablePanicLogging() {
s.isPanicLogEnabled = true
s.logger = getLogger(s)
}

func getLogger(s *Schema) log.Logger {
if s != nil && s.isPanicLogEnabled {
return &log.CustomLogger{true}
}
return &log.DefaultLogger{}
}

// Exec executes the given query with the schema's resolver. It panics if the schema was created
// without a resolver. If the context get cancelled, no further resolvers will be called and a
// the context error will be returned as soon as possible (not immediately).
Expand Down Expand Up @@ -223,6 +237,7 @@ 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)
r.QInfo = fmt.Sprintf("Query: %s\nVariables: %+v\n", queryString, variables)
data, errs := r.Execute(traceCtx, res, op)
finish(errs)

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

func (r *Request) handlePanic(ctx context.Context) {
if value := recover(); value != nil {
r.Logger.LogPanic(ctx, value)
r.Logger.LogPanic(ctx, value, r.QInfo)
r.AddError(makePanicError(value))
}
}
Expand Down Expand Up @@ -177,7 +178,7 @@ 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)
r.Logger.LogPanic(ctx, panicValue, r.QInfo)
err = makePanicError(panicValue)
err.Path = path.toSlice()
}
Expand Down Expand Up @@ -216,20 +217,7 @@ func execFieldSelection(ctx context.Context, r *Request, s *resolvable.Schema, f
err := errors.Errorf("%s", resolverErr)
err.Path = path.toSlice()
err.ResolverError = resolverErr
errs := errors.QueryError{
Message: err.Message,
Locations: err.Locations,
Path: err.Path,
Rule: "",
ResolverError: err.ResolverError,
Extensions: errors.Extensions{
Code: 0,
DeveloperMessage: err.Message,
MoreInfo: fmt.Sprintf("%s", err.Path),
Timestamp: "",
},
}
return &errs
return err
}
} else {
// TODO extract out unwrapping ptr logic to a common place
Expand Down
26 changes: 23 additions & 3 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,42 @@ package log

import (
"context"
"fmt"
"log"
"runtime"
)

// 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", value, buf)
}

// CustomLogger is the custom logger for use with gqlserver custome config to log panics that occur during query execution
type CustomLogger struct{
VerbosePanicLog bool
}

// LogPanic is used to log recovered panic values that occur during query execution
func (l *CustomLogger) LogPanic(_ context.Context, value interface{}, info string) {
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
msg := fmt.Sprintf("graphql: panic occurred: %v\n%s", value, buf)
if l.VerbosePanicLog {
msg = fmt.Sprintf("%s\n\n%s", msg, info)
}

log.Printf(msg)
}