@@ -515,18 +515,19 @@ func TestNullsOutErrorSubtrees(t *testing.T) {
515
515
"sync" : "sync" ,
516
516
"syncError" : nil ,
517
517
}
518
- expectedErrors := []gqlerrors.FormattedError {
519
- {
520
- Message : "Error getting syncError" ,
521
- Locations : []location.SourceLocation {
522
- {
523
- Line : 3 , Column : 7 ,
524
- },
525
- },
526
- Path : []interface {}{
527
- "syncError" ,
518
+ originalError := errors .New ("Error getting syncError" )
519
+ expectedErrors := []gqlerrors.FormattedError {gqlerrors .FormatError (gqlerrors.Error {
520
+ Message : originalError .Error (),
521
+ Locations : []location.SourceLocation {
522
+ {
523
+ Line : 3 , Column : 7 ,
528
524
},
529
525
},
526
+ Path : []interface {}{
527
+ "syncError" ,
528
+ },
529
+ OriginalError : originalError ,
530
+ }),
530
531
}
531
532
532
533
data := map [string ]interface {}{
@@ -1296,6 +1297,7 @@ func TestFailsWhenAnIsTypeOfCheckIsNotMet(t *testing.T) {
1296
1297
},
1297
1298
}
1298
1299
1300
+ originalError := gqlerrors .NewFormattedError (`Expected value of type "SpecialType" but got: graphql_test.testNotSpecialType.` )
1299
1301
expected := & graphql.Result {
1300
1302
Data : map [string ]interface {}{
1301
1303
"specials" : []interface {}{
@@ -1305,21 +1307,20 @@ func TestFailsWhenAnIsTypeOfCheckIsNotMet(t *testing.T) {
1305
1307
nil ,
1306
1308
},
1307
1309
},
1308
- Errors : []gqlerrors.FormattedError {
1309
- {
1310
- Message : `Expected value of type "SpecialType" but got: graphql_test.testNotSpecialType.` ,
1311
- Locations : []location.SourceLocation {
1312
- {
1313
- Line : 1 ,
1314
- Column : 3 ,
1315
- },
1316
- },
1317
- Path : []interface {}{
1318
- "specials" ,
1319
- 1 ,
1310
+ Errors : []gqlerrors.FormattedError {gqlerrors .FormatError (gqlerrors.Error {
1311
+ Message : originalError .Message ,
1312
+ Locations : []location.SourceLocation {
1313
+ {
1314
+ Line : 1 ,
1315
+ Column : 3 ,
1320
1316
},
1321
1317
},
1322
- },
1318
+ Path : []interface {}{
1319
+ "specials" ,
1320
+ 1 ,
1321
+ },
1322
+ OriginalError : originalError ,
1323
+ })},
1323
1324
}
1324
1325
1325
1326
specialType := graphql .NewObject (graphql.ObjectConfig {
@@ -2045,7 +2046,7 @@ func (err extendedError) Extensions() map[string]interface{} {
2045
2046
2046
2047
var _ gqlerrors.ExtendedError = & extendedError {}
2047
2048
2048
- 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 {
2049
2050
type Hero struct {
2050
2051
Id string `graphql:"id"`
2051
2052
Name string
@@ -2072,7 +2073,12 @@ func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]int
2072
2073
if hero .Name != "" {
2073
2074
return hero .Name , nil
2074
2075
}
2076
+
2075
2077
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
+
2076
2082
if extensions != nil {
2077
2083
return nil , & extendedError {
2078
2084
error : err ,
@@ -2133,7 +2139,7 @@ func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]int
2133
2139
2134
2140
// http://facebook.github.io/graphql/June2018/#example-bc485
2135
2141
func TestQuery_ErrorPath (t * testing.T ) {
2136
- result := testErrors (t , graphql .String , nil )
2142
+ result := testErrors (t , graphql .String , nil , nil )
2137
2143
2138
2144
assertJSON (t , `{
2139
2145
"errors": [
@@ -2167,7 +2173,7 @@ func TestQuery_ErrorPath(t *testing.T) {
2167
2173
2168
2174
// http://facebook.github.io/graphql/June2018/#example-08b62
2169
2175
func TestQuery_ErrorPathForNonNullField (t * testing.T ) {
2170
- result := testErrors (t , graphql .NewNonNull (graphql .String ), nil )
2176
+ result := testErrors (t , graphql .NewNonNull (graphql .String ), nil , nil )
2171
2177
2172
2178
assertJSON (t , `{
2173
2179
"errors": [
@@ -2201,7 +2207,7 @@ func TestQuery_ErrorExtensions(t *testing.T) {
2201
2207
result := testErrors (t , graphql .NewNonNull (graphql .String ), map [string ]interface {}{
2202
2208
"code" : "CAN_NOT_FETCH_BY_ID" ,
2203
2209
"timestamp" : "Fri Feb 9 14:33:09 UTC 2018" ,
2204
- })
2210
+ }, nil )
2205
2211
2206
2212
assertJSON (t , `{
2207
2213
"errors": [
@@ -2232,3 +2238,70 @@ func TestQuery_ErrorExtensions(t *testing.T) {
2232
2238
}
2233
2239
}` , result )
2234
2240
}
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