1717
1818import { ErrorCode , Connection } from '../../implementation/connection' ;
1919import { internalError } from '../../implementation/error' ;
20- import nodeFetch from 'node-fetch' ;
21-
22- // eslint-disable-next-line @typescript-eslint/no-explicit-any
23- const fetch : typeof window . fetch = nodeFetch as any ;
20+ import nodeFetch , { FetchError } from 'node-fetch' ;
2421
2522/**
2623 * Network layer that works in Node.
@@ -34,6 +31,8 @@ export class FetchConnection implements Connection {
3431 private body_ : string | undefined ;
3532 private headers_ : Headers | undefined ;
3633 private sent_ : boolean = false ;
34+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
35+ private fetch_ : typeof window . fetch = nodeFetch as any ;
3736
3837 constructor ( ) {
3938 this . errorCode_ = ErrorCode . NO_ERROR ;
@@ -50,18 +49,21 @@ export class FetchConnection implements Connection {
5049 }
5150 this . sent_ = true ;
5251
53- return fetch ( url , {
52+ return this . fetch_ ( url , {
5453 method,
5554 headers : headers || { } ,
5655 body
5756 } )
5857 . then ( resp => {
5958 this . headers_ = resp . headers ;
6059 this . statusCode_ = resp . status ;
61- return resp . text ( ) ;
62- } )
63- . then ( body => {
64- this . body_ = body ;
60+ return resp . text ( ) . then ( body => {
61+ this . body_ = body ;
62+ } ) ;
63+ } , ( _err : FetchError ) => {
64+ this . errorCode_ = ErrorCode . NETWORK_ERROR ;
65+ // emulate XHR which sets status to 0 when encountering a network error
66+ this . statusCode_ = 0 ;
6567 } ) ;
6668 }
6769
0 commit comments