17
17
18
18
import { ErrorCode , Connection } from '../../implementation/connection' ;
19
19
import { 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' ;
24
21
25
22
/**
26
23
* Network layer that works in Node.
@@ -34,6 +31,8 @@ export class FetchConnection implements Connection {
34
31
private body_ : string | undefined ;
35
32
private headers_ : Headers | undefined ;
36
33
private sent_ : boolean = false ;
34
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
+ private fetch_ : typeof window . fetch = nodeFetch as any ;
37
36
38
37
constructor ( ) {
39
38
this . errorCode_ = ErrorCode . NO_ERROR ;
@@ -50,18 +49,21 @@ export class FetchConnection implements Connection {
50
49
}
51
50
this . sent_ = true ;
52
51
53
- return fetch ( url , {
52
+ return this . fetch_ ( url , {
54
53
method,
55
54
headers : headers || { } ,
56
55
body
57
56
} )
58
57
. then ( resp => {
59
58
this . headers_ = resp . headers ;
60
59
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 ;
65
67
} ) ;
66
68
}
67
69
0 commit comments