Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

5argon
Copy link

@5argon 5argon commented Jan 31, 2021

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

@5argon 5argon changed the title Fix possible runtime error in isAxiosError.js type guard Fixing possible runtime error in isAxiosError.js type guard Jan 31, 2021
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);

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?

Suggested change
return (typeof payload === 'object') && (payload !== null) && (payload.isAxiosError === true);
return !!payload && (typeof payload === 'object') && (payload.isAxiosError === true);

@crush12132
Copy link

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?

@johachi
Copy link
Contributor

johachi commented Apr 17, 2021

There is already an earlier open PR for this: #3546

@johachi
Copy link
Contributor

johachi commented Apr 19, 2021

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?

@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
}}

@jasonsaayman
Copy link
Member

Closed in favour of #3546

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants