@@ -2046,7 +2046,7 @@ func (err extendedError) Extensions() map[string]interface{} {
2046
2046
2047
2047
var _ gqlerrors.ExtendedError = & extendedError {}
2048
2048
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 {
2050
2050
type Hero struct {
2051
2051
Id string `graphql:"id"`
2052
2052
Name string
@@ -2073,7 +2073,12 @@ func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]int
2073
2073
if hero .Name != "" {
2074
2074
return hero .Name , nil
2075
2075
}
2076
+
2076
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
+
2077
2082
if extensions != nil {
2078
2083
return nil , & extendedError {
2079
2084
error : err ,
@@ -2134,7 +2139,7 @@ func testErrors(t *testing.T, nameType graphql.Output, extensions map[string]int
2134
2139
2135
2140
// http://facebook.github.io/graphql/June2018/#example-bc485
2136
2141
func TestQuery_ErrorPath (t * testing.T ) {
2137
- result := testErrors (t , graphql .String , nil )
2142
+ result := testErrors (t , graphql .String , nil , nil )
2138
2143
2139
2144
assertJSON (t , `{
2140
2145
"errors": [
@@ -2168,7 +2173,7 @@ func TestQuery_ErrorPath(t *testing.T) {
2168
2173
2169
2174
// http://facebook.github.io/graphql/June2018/#example-08b62
2170
2175
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 )
2172
2177
2173
2178
assertJSON (t , `{
2174
2179
"errors": [
@@ -2202,7 +2207,7 @@ func TestQuery_ErrorExtensions(t *testing.T) {
2202
2207
result := testErrors (t , graphql .NewNonNull (graphql .String ), map [string ]interface {}{
2203
2208
"code" : "CAN_NOT_FETCH_BY_ID" ,
2204
2209
"timestamp" : "Fri Feb 9 14:33:09 UTC 2018" ,
2205
- })
2210
+ }, nil )
2206
2211
2207
2212
assertJSON (t , `{
2208
2213
"errors": [
@@ -2233,3 +2238,70 @@ func TestQuery_ErrorExtensions(t *testing.T) {
2233
2238
}
2234
2239
}` , result )
2235
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