New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixing possible runtime error in isAxiosError.js type guard #3613
Conversation
AxiosError type guard can cause runtime error if payload is a `null`, since `null` also passes the check `typeof payload === 'object'`. Then the code cannot access `.isAxiosError` of a `null`. ``` TypeError: Cannot read property 'isAxiosError' of null ```
@@ -7,5 +7,5 @@ | |||
* @returns {boolean} True if the payload is an error thrown by Axios, otherwise false | |||
*/ | |||
module.exports = function isAxiosError(payload) { | |||
return (typeof payload === 'object') && (payload.isAxiosError === true); | |||
return (typeof payload === 'object') && (payload !== null) && (payload.isAxiosError === true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we just check if payload
is truthy?
return (typeof payload === 'object') && (payload !== null) && (payload.isAxiosError === true); | |
return !!payload && (typeof payload === 'object') && (payload.isAxiosError === true); |
I also encountered this problem. Whenever Google Chrome runs Ajax-related code, just add debugger and click Next to jump directly to the isAxiosError.js page. How can I solve it? |
There is already an earlier open PR for this: #3546 |
@crush12132 Hard to know without looking at your code, but you probably just need to check if the value is not null before you check if it is an Axios error. Something like if (error !== null && axios.isAxiosError(error) {
// handle the axios error
} or if (!axios.isAxiosError(error) || error === null) {
// handle the non-axios error
}} |
Closed in favour of #3546 |
AxiosError type guard can cause runtime error if payload is a
null
, sincenull
also passes the checktypeof payload === 'object'
. Then the code cannot access.isAxiosError
of anull
.