Skip to content

Commit 72c3dcc

Browse files
committed
ref:updated error logic
Signed-off-by: Amitkanswal <[email protected]>
1 parent 0fbf787 commit 72c3dcc

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/utils/utils.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,31 @@ export function sanitizeResponseHeader(axiosHeaders) {
3939
return fetchHeaders;
4040
};
4141
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+
}
6367
}
6468
export function formatAppRegion(region: string): RegionType {
6569
return region ?? Region.UNKNOWN;

0 commit comments

Comments
 (0)