Skip to content

handler: consolidates FormatErrorFn #59

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

Merged
merged 1 commit into from
Dec 10, 2018
Merged
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
6 changes: 3 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Handler struct {
playground bool
rootObjectFn RootObjectFn
resultCallbackFn ResultCallbackFn
formatErrorFn func(err gqlerrors.FormattedError) gqlerrors.FormattedError
formatErrorFn func(err error) gqlerrors.FormattedError
}

type RequestOptions struct {
Expand Down Expand Up @@ -144,7 +144,7 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
if formatErrorFn := h.formatErrorFn; formatErrorFn != nil && len(result.Errors) > 0 {
formatted := make([]gqlerrors.FormattedError, len(result.Errors))
for i, formattedError := range result.Errors {
formatted[i] = formatErrorFn(formattedError)
formatted[i] = formatErrorFn(formattedError.OriginalError())
}
result.Errors = formatted
}
Expand Down Expand Up @@ -203,7 +203,7 @@ type Config struct {
Playground bool
RootObjectFn RootObjectFn
ResultCallbackFn ResultCallbackFn
FormatErrorFn func(err gqlerrors.FormattedError) gqlerrors.FormattedError
FormatErrorFn func(err error) gqlerrors.FormattedError
}

func NewConfig() *Config {
Expand Down
30 changes: 10 additions & 20 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handler_test

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -215,15 +214,15 @@ func TestHandler_BasicQuery_WithRootObjFn(t *testing.T) {
}

type customError struct {
error
message string
}

func (e customError) Error() string {
return e.error.Error()
return fmt.Sprintf("%s", e.message)
}

func TestHandler_BasicQuery_WithFormatErrorFn(t *testing.T) {
resolverError := customError{error: errors.New("resolver error")}
resolverError := customError{message: "resolver error"}
myNameQuery := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
Expand Down Expand Up @@ -252,9 +251,6 @@ func TestHandler_BasicQuery_WithFormatErrorFn(t *testing.T) {
},
},
Path: []interface{}{"name"},
Extensions: map[string]interface{}{
"fromFormatFn": "FROM_FORMAT_FN",
},
}

expected := &graphql.Result{
Expand All @@ -271,22 +267,16 @@ func TestHandler_BasicQuery_WithFormatErrorFn(t *testing.T) {
h := handler.New(&handler.Config{
Schema: &myNameSchema,
Pretty: true,
FormatErrorFn: func(err gqlerrors.FormattedError) gqlerrors.FormattedError {
FormatErrorFn: func(err error) gqlerrors.FormattedError {
formatErrorFnCalled = true
originalError := err.OriginalError()
switch errType := originalError.(type) {
case customError:
var formatted gqlerrors.FormattedError
switch err := err.(type) {
case *gqlerrors.Error:
formatted = gqlerrors.FormatError(err)
default:
t.Fatalf("unexpected error type: %v", reflect.TypeOf(errType))
}
return gqlerrors.FormattedError{
Message: err.Message,
Locations: err.Locations,
Path: err.Path,
Extensions: map[string]interface{}{
"fromFormatFn": "FROM_FORMAT_FN",
},
t.Fatalf("unexpected error type: %v", reflect.TypeOf(err))
}
return formatted
},
})
result, resp := executeTest(t, h, req)
Expand Down