diff --git a/executor_test.go b/executor_test.go index 7b5779bc..62d7c682 100644 --- a/executor_test.go +++ b/executor_test.go @@ -2046,7 +2046,7 @@ func (err extendedError) Extensions() map[string]interface{} { var _ gqlerrors.ExtendedError = &extendedError{} -func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]interface{}) *graphql.Result { +func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]interface{}, formatErrorFn func(err error) error) *graphql.Result { type Hero struct { Id string `graphql:"id"` Name string @@ -2073,7 +2073,12 @@ func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]int if hero.Name != "" { return hero.Name, nil } + err := fmt.Errorf("Name for character with ID %v could not be fetched.", hero.Id) + if formatErrorFn != nil { + err = formatErrorFn(err) + } + if extensions != nil { return nil, &extendedError{ error: err, @@ -2134,7 +2139,7 @@ func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]int // http://facebook.github.io/graphql/June2018/#example-bc485 func TestQuery_ErrorPath(t *testing.T) { - result := testErrors(t, graphql.String, nil) + result := testErrors(t, graphql.String, nil, nil) assertJSON(t, `{ "errors": [ @@ -2168,7 +2173,7 @@ func TestQuery_ErrorPath(t *testing.T) { // http://facebook.github.io/graphql/June2018/#example-08b62 func TestQuery_ErrorPathForNonNullField(t *testing.T) { - result := testErrors(t, graphql.NewNonNull(graphql.String), nil) + result := testErrors(t, graphql.NewNonNull(graphql.String), nil, nil) assertJSON(t, `{ "errors": [ @@ -2202,7 +2207,7 @@ func TestQuery_ErrorExtensions(t *testing.T) { result := testErrors(t, graphql.NewNonNull(graphql.String), map[string]interface{}{ "code": "CAN_NOT_FETCH_BY_ID", "timestamp": "Fri Feb 9 14:33:09 UTC 2018", - }) + }, nil) assertJSON(t, `{ "errors": [ @@ -2233,3 +2238,70 @@ func TestQuery_ErrorExtensions(t *testing.T) { } }`, result) } + +func TestQuery_OriginalErrorBuiltin(t *testing.T) { + result := testErrors(t, graphql.String, nil, nil) + originalError := result.Errors[0].OriginalError() + switch originalError.(type) { + case error: + default: + t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError)) + } +} + +func TestQuery_OriginalErrorExtended(t *testing.T) { + result := testErrors(t, graphql.String, map[string]interface{}{ + "code": "CAN_NOT_FETCH_BY_ID", + }, nil) + originalError := result.Errors[0].OriginalError() + switch originalError.(type) { + case *extendedError: + case extendedError: + default: + t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError)) + } +} + +type customError struct { + error +} + +func (e customError) Error() string { + return e.error.Error() +} + +func TestQuery_OriginalErrorCustom(t *testing.T) { + result := testErrors(t, graphql.String, nil, func(err error) error { + return customError{error: err} + }) + originalError := result.Errors[0].OriginalError() + switch originalError.(type) { + case customError: + default: + t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError)) + } +} + +func TestQuery_OriginalErrorCustomPtr(t *testing.T) { + result := testErrors(t, graphql.String, nil, func(err error) error { + return &customError{error: err} + }) + originalError := result.Errors[0].OriginalError() + switch originalError.(type) { + case *customError: + default: + t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError)) + } +} + +func TestQuery_OriginalErrorPanic(t *testing.T) { + result := testErrors(t, graphql.String, nil, func(err error) error { + panic(errors.New("panic error")) + }) + originalError := result.Errors[0].OriginalError() + switch originalError.(type) { + case error: + default: + t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError)) + } +}