From a2efeec33ee0dffa3a7edfbd05183003525ad7e3 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 18 May 2020 15:25:43 -0400 Subject: [PATCH] API: gracefully handle context errors --- api/server.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/api/server.go b/api/server.go index ac64946..f393121 100644 --- a/api/server.go +++ b/api/server.go @@ -6,6 +6,7 @@ import ( "crypto/rand" "database/sql" "encoding/binary" + "errors" "fmt" "log" "net/http" @@ -111,11 +112,20 @@ func main() { srv := handler.GraphQL( api.NewExecutableSchema(gqlConfig), handler.ComplexityLimit(complexity), - handler.RecoverFunc(func(ctx context.Context, origErr interface{}) error { - if _, ok := origErr.(error); !ok { + handler.RecoverFunc(func(ctx context.Context, _origErr interface{}) error { + var origErr error + if origErr, ok = _origErr.(error); !ok { log.Printf("Unexpected error in recover: %v\n", origErr) return fmt.Errorf("internal system error") } + + if errors.Is(origErr, context.Canceled) { + return origErr + } + if errors.Is(origErr, context.DeadlineExceeded) { + return origErr + } + stack := make([]byte, 32768) // 32 KiB i := runtime.Stack(stack, false) log.Println(string(stack[:i])) -- 2.38.4