Skip to content

Commit

Permalink
bugfix: Default Recover func should return gqlerror.Error
Browse files Browse the repository at this point in the history
  • Loading branch information
j2gg0s committed Nov 15, 2020
1 parent 0e12bfb commit d0d5f7d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
22 changes: 22 additions & 0 deletions graphql/handler/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler_test

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -162,6 +163,27 @@ func TestErrorServer(t *testing.T) {
})
}

type panicTransport struct{}

func (t panicTransport) Supports(r *http.Request) bool {
return true
}

func (h panicTransport) Do(w http.ResponseWriter, r *http.Request, exec graphql.GraphExecutor) {
panic(fmt.Errorf("panic in transport"))
}

func TestRecover(t *testing.T) {
srv := testserver.New()
srv.AddTransport(&panicTransport{})

t.Run("recover from panic", func(t *testing.T) {
resp := get(srv, "/foo?query={name}")

assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
})
}

func get(handler http.Handler, target string) *httptest.ResponseRecorder {
r := httptest.NewRequest("GET", target, nil)
w := httptest.NewRecorder()
Expand Down
5 changes: 3 additions & 2 deletions graphql/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package graphql

import (
"context"
"errors"
"fmt"
"os"
"runtime/debug"

"github.com/vektah/gqlparser/v2/gqlerror"
)

type RecoverFunc func(ctx context.Context, err interface{}) (userMessage error)
Expand All @@ -15,5 +16,5 @@ func DefaultRecover(ctx context.Context, err interface{}) error {
fmt.Fprintln(os.Stderr)
debug.PrintStack()

return errors.New("internal system error")
return gqlerror.Errorf("internal system error")
}

0 comments on commit d0d5f7d

Please sign in to comment.