Skip to content

Commit bece7a6

Browse files
authored
Merge pull request #59 from graphql-go/updated-format-error-fn
handler: consolidates FormatErrorFn
2 parents 12f536e + d08c809 commit bece7a6

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Handler struct {
2929
playground bool
3030
rootObjectFn RootObjectFn
3131
resultCallbackFn ResultCallbackFn
32-
formatErrorFn func(err gqlerrors.FormattedError) gqlerrors.FormattedError
32+
formatErrorFn func(err error) gqlerrors.FormattedError
3333
}
3434

3535
type RequestOptions struct {
@@ -144,7 +144,7 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
144144
if formatErrorFn := h.formatErrorFn; formatErrorFn != nil && len(result.Errors) > 0 {
145145
formatted := make([]gqlerrors.FormattedError, len(result.Errors))
146146
for i, formattedError := range result.Errors {
147-
formatted[i] = formatErrorFn(formattedError)
147+
formatted[i] = formatErrorFn(formattedError.OriginalError())
148148
}
149149
result.Errors = formatted
150150
}
@@ -203,7 +203,7 @@ type Config struct {
203203
Playground bool
204204
RootObjectFn RootObjectFn
205205
ResultCallbackFn ResultCallbackFn
206-
FormatErrorFn func(err gqlerrors.FormattedError) gqlerrors.FormattedError
206+
FormatErrorFn func(err error) gqlerrors.FormattedError
207207
}
208208

209209
func NewConfig() *Config {

handler_test.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package handler_test
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"io/ioutil"
87
"net/http"
@@ -215,15 +214,15 @@ func TestHandler_BasicQuery_WithRootObjFn(t *testing.T) {
215214
}
216215

217216
type customError struct {
218-
error
217+
message string
219218
}
220219

221220
func (e customError) Error() string {
222-
return e.error.Error()
221+
return fmt.Sprintf("%s", e.message)
223222
}
224223

225224
func TestHandler_BasicQuery_WithFormatErrorFn(t *testing.T) {
226-
resolverError := customError{error: errors.New("resolver error")}
225+
resolverError := customError{message: "resolver error"}
227226
myNameQuery := graphql.NewObject(graphql.ObjectConfig{
228227
Name: "Query",
229228
Fields: graphql.Fields{
@@ -252,9 +251,6 @@ func TestHandler_BasicQuery_WithFormatErrorFn(t *testing.T) {
252251
},
253252
},
254253
Path: []interface{}{"name"},
255-
Extensions: map[string]interface{}{
256-
"fromFormatFn": "FROM_FORMAT_FN",
257-
},
258254
}
259255

260256
expected := &graphql.Result{
@@ -271,22 +267,16 @@ func TestHandler_BasicQuery_WithFormatErrorFn(t *testing.T) {
271267
h := handler.New(&handler.Config{
272268
Schema: &myNameSchema,
273269
Pretty: true,
274-
FormatErrorFn: func(err gqlerrors.FormattedError) gqlerrors.FormattedError {
270+
FormatErrorFn: func(err error) gqlerrors.FormattedError {
275271
formatErrorFnCalled = true
276-
originalError := err.OriginalError()
277-
switch errType := originalError.(type) {
278-
case customError:
272+
var formatted gqlerrors.FormattedError
273+
switch err := err.(type) {
274+
case *gqlerrors.Error:
275+
formatted = gqlerrors.FormatError(err)
279276
default:
280-
t.Fatalf("unexpected error type: %v", reflect.TypeOf(errType))
281-
}
282-
return gqlerrors.FormattedError{
283-
Message: err.Message,
284-
Locations: err.Locations,
285-
Path: err.Path,
286-
Extensions: map[string]interface{}{
287-
"fromFormatFn": "FROM_FORMAT_FN",
288-
},
277+
t.Fatalf("unexpected error type: %v", reflect.TypeOf(err))
289278
}
279+
return formatted
290280
},
291281
})
292282
result, resp := executeTest(t, h, req)

0 commit comments

Comments
 (0)