File tree Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,15 @@ export class RequestError extends Error {
36
36
}
37
37
38
38
this . name = "HttpError" ;
39
- this . status = statusCode ;
39
+
40
+ // Implicit coercion to number if statusCode is a string
41
+ this . status = Number . parseInt ( statusCode as unknown as string ) ;
42
+
43
+ // If status is not equal to itself, then it is NaN
44
+ // we set then the status to 0
45
+ if ( Number . isNaN ( this . status ) ) {
46
+ this . status = 0 ;
47
+ }
40
48
41
49
if ( "response" in options ) {
42
50
this . response = options . response ;
Original file line number Diff line number Diff line change @@ -34,6 +34,13 @@ describe("RequestError", () => {
34
34
test ( "sets .status" , ( ) => {
35
35
expect ( new RequestError ( "test" , 123 , mockOptions ) . status ) . toEqual ( 123 ) ;
36
36
expect ( new RequestError ( "test" , 404 , mockOptions ) . status ) . toEqual ( 404 ) ;
37
+ // @ts -expect-error
38
+ expect ( new RequestError ( "test" , "404" , mockOptions ) . status ) . toEqual ( 404 ) ;
39
+ expect ( new RequestError ( "test" , NaN , mockOptions ) . status ) . toEqual ( 0 ) ;
40
+ // @ts -expect-error
41
+ expect ( new RequestError ( "test" , [ ] , mockOptions ) . status ) . toEqual ( 0 ) ;
42
+ // @ts -expect-error
43
+ expect ( new RequestError ( "test" , new Date ( ) , mockOptions ) . status ) . toEqual ( 0 ) ;
37
44
} ) ;
38
45
39
46
test ( "sets .request" , ( ) => {
You can’t perform that action at this time.
0 commit comments