@@ -39,27 +39,31 @@ export function sanitizeResponseHeader(axiosHeaders) {
39
39
return fetchHeaders ;
40
40
} ;
41
41
export const handleApiError = ( error : AxiosResponse | AxiosError ) : Response => {
42
- // Extract relevant information from the error
43
- const isServerError = ( error as AxiosResponse ) . status >= 500 ;
44
- const responseBody = isServerError
45
- ? ( error as AxiosError ) . stack || "Internal Server Error"
46
- : ( error as AxiosResponse ) . data || "An error occurred" ;
47
-
48
- const status = ( error as AxiosResponse ) . status || 500 ;
49
- const statusText =
50
- isServerError
51
- ? ( error as AxiosError ) . message || "Internal Server Error"
52
- : ( error as AxiosResponse ) . statusText || "Error" ;
53
-
54
- const headers = new Headers (
55
- sanitizeResponseHeader ( ( error as AxiosResponse ) . headers || { } )
56
- ) ;
57
-
58
- return new Response ( JSON . stringify ( responseBody ) , {
59
- status,
60
- statusText,
61
- headers,
62
- } ) ;
42
+ if ( isAxiosError ( error ) ) {
43
+ const isServerError = ( error ?. status ?? 0 ) >= 500 ;
44
+ const responseBody = isServerError
45
+ ? error . stack || "Internal Server Error"
46
+ : ( error as unknown as AxiosResponse ) ?. data || "An error occurred" ;
47
+ const status = error ?. status || 500 ;
48
+ const statusText =
49
+ isServerError
50
+ ? error . message || "Internal Server Error"
51
+ : ( error as unknown as AxiosResponse ) ?. statusText || "Error" ;
52
+ const headers = new Headers (
53
+ sanitizeResponseHeader ( error . response ?. headers || { } )
54
+ ) ;
55
+ return new Response ( JSON . stringify ( responseBody ) , {
56
+ status,
57
+ statusText,
58
+ headers,
59
+ } ) ;
60
+ } else {
61
+ const responseBody = error . statusText || "An error occurred" ;
62
+ return new Response ( JSON . stringify ( responseBody ) , {
63
+ status : 500 ,
64
+ statusText : "Internal Server Error" ,
65
+ } ) ;
66
+ }
63
67
}
64
68
export function formatAppRegion ( region : string ) : RegionType {
65
69
return region ?? Region . UNKNOWN ;
0 commit comments