~edwargix/git.sr.ht

a2efeec33ee0dffa3a7edfbd05183003525ad7e3 — Drew DeVault 5 years ago fd0fc1f
API: gracefully handle context errors
1 files changed, 12 insertions(+), 2 deletions(-)

M api/server.go
M api/server.go => api/server.go +12 -2
@@ 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]))