File tree 2 files changed +29
-7
lines changed
2 files changed +29
-7
lines changed Original file line number Diff line number Diff line change @@ -136,12 +136,12 @@ export default function fetchWrapper(
136
136
// undici throws a TypeError for network errors
137
137
// and puts the error message in `error.cause`
138
138
// https://github.com/nodejs/undici/blob/e5c9d703e63cd5ad691b8ce26e3f9a81c598f2e3/lib/fetch/index.js#L227
139
- if (
140
- error instanceof TypeError &&
141
- "cause" in error &&
142
- typeof error . cause === "string"
143
- ) {
144
- message = error . cause ;
139
+ if ( error . name === "TypeError" && "cause" in error ) {
140
+ if ( error . cause instanceof Error ) {
141
+ message = error . cause . message ;
142
+ } else if ( typeof error . cause === "string" ) {
143
+ message = error . cause ;
144
+ }
145
145
}
146
146
147
147
throw new RequestError ( message , 500 , {
Original file line number Diff line number Diff line change @@ -439,7 +439,29 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
439
439
} ) ;
440
440
} ) ;
441
441
442
- it ( "Request TypeError error" , ( ) => {
442
+ it ( "Request TypeError error with an Error cause" , ( ) => {
443
+ const mock = fetchMock . sandbox ( ) . get ( "https://127.0.0.1:8/" , {
444
+ throws : Object . assign ( new TypeError ( "fetch failed" ) , {
445
+ cause : new Error ( "bad" ) ,
446
+ } ) ,
447
+ } ) ;
448
+
449
+ // port: 8 // officially unassigned port. See https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
450
+ return request ( "GET https://127.0.0.1:8/" , {
451
+ request : {
452
+ fetch : mock ,
453
+ } ,
454
+ } )
455
+ . then ( ( ) => {
456
+ throw new Error ( "should not resolve" ) ;
457
+ } )
458
+ . catch ( ( error ) => {
459
+ expect ( error . status ) . toEqual ( 500 ) ;
460
+ expect ( error . message ) . toEqual ( "bad" ) ;
461
+ } ) ;
462
+ } ) ;
463
+
464
+ it ( "Request TypeError error with a string cause" , ( ) => {
443
465
const mock = fetchMock . sandbox ( ) . get ( "https://127.0.0.1:8/" , {
444
466
throws : Object . assign ( new TypeError ( "fetch failed" ) , { cause : "bad" } ) ,
445
467
} ) ;
You can’t perform that action at this time.
0 commit comments