Skip to content

Commit 7995f6a

Browse files
committed
executor: adds OriginalError tests
1 parent bfe0f60 commit 7995f6a

File tree

1 file changed

+76
-4
lines changed

1 file changed

+76
-4
lines changed

executor_test.go

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,7 @@ func (err extendedError) Extensions() map[string]interface{} {
20462046

20472047
var _ gqlerrors.ExtendedError = &extendedError{}
20482048

2049-
func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]interface{}) *graphql.Result {
2049+
func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]interface{}, formatErrorFn func(err error) error) *graphql.Result {
20502050
type Hero struct {
20512051
Id string `graphql:"id"`
20522052
Name string
@@ -2073,7 +2073,12 @@ func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]int
20732073
if hero.Name != "" {
20742074
return hero.Name, nil
20752075
}
2076+
20762077
err := fmt.Errorf("Name for character with ID %v could not be fetched.", hero.Id)
2078+
if formatErrorFn != nil {
2079+
err = formatErrorFn(err)
2080+
}
2081+
20772082
if extensions != nil {
20782083
return nil, &extendedError{
20792084
error: err,
@@ -2134,7 +2139,7 @@ func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]int
21342139

21352140
// http://facebook.github.io/graphql/June2018/#example-bc485
21362141
func TestQuery_ErrorPath(t *testing.T) {
2137-
result := testErrors(t, graphql.String, nil)
2142+
result := testErrors(t, graphql.String, nil, nil)
21382143

21392144
assertJSON(t, `{
21402145
"errors": [
@@ -2168,7 +2173,7 @@ func TestQuery_ErrorPath(t *testing.T) {
21682173

21692174
// http://facebook.github.io/graphql/June2018/#example-08b62
21702175
func TestQuery_ErrorPathForNonNullField(t *testing.T) {
2171-
result := testErrors(t, graphql.NewNonNull(graphql.String), nil)
2176+
result := testErrors(t, graphql.NewNonNull(graphql.String), nil, nil)
21722177

21732178
assertJSON(t, `{
21742179
"errors": [
@@ -2202,7 +2207,7 @@ func TestQuery_ErrorExtensions(t *testing.T) {
22022207
result := testErrors(t, graphql.NewNonNull(graphql.String), map[string]interface{}{
22032208
"code": "CAN_NOT_FETCH_BY_ID",
22042209
"timestamp": "Fri Feb 9 14:33:09 UTC 2018",
2205-
})
2210+
}, nil)
22062211

22072212
assertJSON(t, `{
22082213
"errors": [
@@ -2233,3 +2238,70 @@ func TestQuery_ErrorExtensions(t *testing.T) {
22332238
}
22342239
}`, result)
22352240
}
2241+
2242+
func TestQuery_OriginalErrorBuiltin(t *testing.T) {
2243+
result := testErrors(t, graphql.String, nil, nil)
2244+
originalError := result.Errors[0].OriginalError()
2245+
switch originalError.(type) {
2246+
case error:
2247+
default:
2248+
t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError))
2249+
}
2250+
}
2251+
2252+
func TestQuery_OriginalErrorExtended(t *testing.T) {
2253+
result := testErrors(t, graphql.String, map[string]interface{}{
2254+
"code": "CAN_NOT_FETCH_BY_ID",
2255+
}, nil)
2256+
originalError := result.Errors[0].OriginalError()
2257+
switch originalError.(type) {
2258+
case *extendedError:
2259+
case extendedError:
2260+
default:
2261+
t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError))
2262+
}
2263+
}
2264+
2265+
type customError struct {
2266+
error
2267+
}
2268+
2269+
func (e customError) Error() string {
2270+
return e.error.Error()
2271+
}
2272+
2273+
func TestQuery_OriginalErrorCustom(t *testing.T) {
2274+
result := testErrors(t, graphql.String, nil, func(err error) error {
2275+
return customError{error: err}
2276+
})
2277+
originalError := result.Errors[0].OriginalError()
2278+
switch originalError.(type) {
2279+
case customError:
2280+
default:
2281+
t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError))
2282+
}
2283+
}
2284+
2285+
func TestQuery_OriginalErrorCustomPtr(t *testing.T) {
2286+
result := testErrors(t, graphql.String, nil, func(err error) error {
2287+
return &customError{error: err}
2288+
})
2289+
originalError := result.Errors[0].OriginalError()
2290+
switch originalError.(type) {
2291+
case *customError:
2292+
default:
2293+
t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError))
2294+
}
2295+
}
2296+
2297+
func TestQuery_OriginalErrorPanic(t *testing.T) {
2298+
result := testErrors(t, graphql.String, nil, func(err error) error {
2299+
panic(errors.New("panic error"))
2300+
})
2301+
originalError := result.Errors[0].OriginalError()
2302+
switch originalError.(type) {
2303+
case error:
2304+
default:
2305+
t.Fatalf("unexpected error: %v", reflect.TypeOf(originalError))
2306+
}
2307+
}

0 commit comments

Comments
 (0)