Skip to content

Commit

Permalink
Merge pull request #59 from graphql-go/updated-format-error-fn
Browse files Browse the repository at this point in the history
handler: consolidates FormatErrorFn
  • Loading branch information
chris-ramon authored Dec 10, 2018
2 parents 12f536e + d08c809 commit bece7a6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
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

0 comments on commit bece7a6

Please sign in to comment.