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

Adding a type guard for `AxiosError` #2949

Merged
merged 13 commits into from Nov 3, 2020
Merged

Conversation

@JasonHK
Copy link
Contributor

@JasonHK JasonHK commented May 6, 2020

This type guard allows TypeScript users to use the AxiosError interface without typecasting.

Usage

import axios, { isAxiosError } from "axios";

axios.get("/endpoint")
    .catch(
        (error) =>
        {
            if (isAxiosError(error)) { ... }
        });

Fix #1863

Copy link
Collaborator

@jasonsaayman jasonsaayman left a comment

Looks good lets add it onto v0.20

@jasonsaayman jasonsaayman added this to In progress in v0.20.0 via automation May 6, 2020
@jasonsaayman jasonsaayman added this to the v0.20.0 milestone May 6, 2020
JasonHK added 5 commits May 26, 2020
@jasonsaayman jasonsaayman added the v0.20.0 label Jun 9, 2020
JasonHK added 3 commits Jun 11, 2020
@jasonsaayman jasonsaayman moved this from In progress to Reviewer approved in v0.20.0 Aug 25, 2020
@jasonsaayman jasonsaayman moved this from Reviewer approved to Done in v0.20.0 Aug 25, 2020
@jasonsaayman jasonsaayman removed this from the v0.20.0 milestone Aug 25, 2020
@jasonsaayman jasonsaayman removed the v0.20.1 label Sep 21, 2020
@JasonHK JasonHK requested a review from jasonsaayman Nov 3, 2020
@jasonsaayman jasonsaayman added this to the v0.21.1 milestone Nov 3, 2020
Copy link
Collaborator

@jasonsaayman jasonsaayman left a comment

This looks good and having tests is great 👍

@jasonsaayman jasonsaayman merged commit f472e5d into axios:master Nov 3, 2020
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@JasonHK JasonHK deleted the JasonHK:feat/is-axios-error branch Nov 3, 2020
@jasonsaayman jasonsaayman added this to Done in v0.21.1 Nov 19, 2020
* @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);

This comment has been minimized.

@BlackHole1

BlackHole1 Jan 7, 2021

typeof null === 'object' => true

Should be changed to:

({}).toString.call(payload) === '[obejct Object]'

This comment has been minimized.

@BlackHole1

BlackHole1 Jan 7, 2021

Is it necessary to make a strict inspection here? @jasonsaayman @JasonHK

This comment has been minimized.

@JasonHK

JasonHK Jan 7, 2021
Author Contributor

Oops, I totally forgot about this. But because this PR had already been merged, I will open another PR to fix this.

This comment has been minimized.

This comment has been minimized.

@BlackHole1

BlackHole1 Jan 14, 2021

I have submitted the relevant PR: #3546

@boy51
Copy link

@boy51 boy51 commented Jan 13, 2021

Ummm... Not sure if I'm just stupid but I updated axios and it seems the import doesn't work?

import axios, { isAxiosError } from "axios";
"../../../../../../node_modules/axios"' has no exported member named 'isAxiosError'. Did you mean 'AxiosError'?  TS2724

edit: I restarted editor multiple times, restarted script, deleted node_modules. It seems the import doesn't work. But I can use method axios.isAxiosError

@JasonHK
Copy link
Contributor Author

@JasonHK JasonHK commented Jan 14, 2021

Ummm... Not sure if I'm just stupid but I updated axios and it seems the import doesn't work?

import axios, { isAxiosError } from "axios";
"../../../../../../node_modules/axios"' has no exported member named 'isAxiosError'. Did you mean 'AxiosError'?  TS2724

edit: I restarted editor multiple times, restarted script, deleted node_modules. It seems the import doesn't work. But I can use method axios.isAxiosError

@boy51 No, it's not your problem. I forgot to define the function in index.d.ts.

@BlackHole1
Copy link

@BlackHole1 BlackHole1 commented Jan 14, 2021

@JasonHK This is not the case, the method itself should not be exposed separately. isAxiosError has been defined in AxiosStatic. This should be a usage problem

@TJGhinder
Copy link

@TJGhinder TJGhinder commented Feb 1, 2021

^I had an issue similar to boy51. My build on Heroku was failing, until I pinned my axios version as 0.21.0. I haven't tried a build since the latest update (this is an older project). My package.json had axios "^0.21.1" and Heroku was giving me an error about not being able to resolve "./helpers/isAxiosError" within Axios which was causing my build to fail.

On my local machine, the builds worked, but in the Heroku cloud it was not working. As I said the fix was to pin version 0.21.0 and the issue is no longer present. I cannot build with 0.21.1 in my production environment (Not an issue for me atm, but worth acknowledging that this seems to be an issue, still).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
v0.20.0
  
Done
Linked issues

Successfully merging this pull request may close these issues.

6 participants