Skip to content

Commit e955d9b

Browse files
committed
fix: Improve error type detection (#5)
1 parent 9974edb commit e955d9b

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/axiosBetterStacktrace.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ declare module 'axios' {
1616
}
1717
}
1818

19+
// toString used instead of instanceOf for detecting error type to prevent problem mentioned in issue #5
20+
const isError = (error: unknown): error is Error =>
21+
Object.prototype.toString.call(error) === '[object Error]';
22+
1923
const isAxiosError = (error: unknown): error is AxiosError =>
20-
error instanceof Error && (error as AxiosError).isAxiosError;
24+
isError(error) && (error as AxiosError).isAxiosError;
2125

2226
const axiosMethods = [
2327
'request',
@@ -57,21 +61,19 @@ const axiosBetterStacktrace = (axiosInstance?: AxiosInstance, opts: { errorMsg?:
5761
// enhance original response error with a topmostError stack trace
5862
const responseErrorInterceptorId = axiosInstance.interceptors.response.use(
5963
(response) => {
60-
if (response.config && response.config.topmostError instanceof Error) {
64+
if (response.config && isError(response.config.topmostError)) {
6165
// remove topmostError to not clutter config and expose it to other interceptors down the chain
6266
delete response.config.topmostError;
6367
}
6468

6569
return response;
6670
},
6771
(error: unknown) => {
68-
if (isAxiosError(error) && error.config && error.config.topmostError instanceof Error) {
72+
if (isAxiosError(error) && error.config && isError(error.config.topmostError)) {
6973
error.originalStack = error.stack;
7074
error.stack = `${error.stack}\n${error.config.topmostError.stack}`;
7175

7276
delete error.config.topmostError;
73-
74-
throw error;
7577
}
7678

7779
throw error;

0 commit comments

Comments
 (0)