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

Add AxiosException class #2013

Closed
ruscon opened this issue Feb 24, 2019 · 20 comments
Closed

Add AxiosException class #2013

ruscon opened this issue Feb 24, 2019 · 20 comments

Comments

@ruscon
Copy link

ruscon commented Feb 24, 2019

Is your feature request related to a problem? Please describe.
I use typescript.
How to verify that I received exactly axios error?

Describe the solution you'd like
Add custom error class: AxiosException, which is inherited from the standard Error class

Example

try {
  axios.get('...')
}
catch(err) {
  if (err instanceof AxiosException) {
      console.error(err.response);
  }
}
@chinesedfan
Copy link
Collaborator

How about checking error.isAxiosError directly?

@ruscon
Copy link
Author

ruscon commented Feb 28, 2019

@chinesedfan
Typescript needs typing.
The typification of the class Error does not know anything about this property.
and the interface in this case also does not work normally.
Those, the only option is a class

@chinesedfan
Copy link
Collaborator

@ruscon I think you can use type assertion.

import axios, {AxiosError} from 'axios';

try {
  axios.get('...')
}
catch(err) {
  if ((<any>err).isAxiosError) { // check to make sure type assertion is right
      const e = <AxiosError>error;
      console.error(e.response);
  }
}

@ruscon
Copy link
Author

ruscon commented Feb 28, 2019

@chinesedfan So you can do something like this:

import axios, {AxiosError} from 'axios';

try {
    axios.get('...')
}
catch(err) {
    if (err.isAxiosError) { // no types
        const e: AxiosError = error;
        console.error(e.response);
    }
}

But this is a bad approach for both js and ts.

@ruscon
Copy link
Author

ruscon commented Feb 28, 2019

@chinesedfan
Copy link
Collaborator

But this is a bad approach for both js and ts.

@ruscon I agree a bit in ts, but not in js. There are so many duck types in js. I think that's why ts designs type assertion. Your AxiosException approach is a standard but heavy way. If someday axios were totally refactored, I will stand in your side. Good luck!

@ruscon
Copy link
Author

ruscon commented Feb 28, 2019

@chinesedfan heavy way ?

@chinesedfan
Copy link
Collaborator

@ruscon heavy means extra changes. I am not a native speaker and don't know whether it has strange meanings in English.

@ruscon
Copy link
Author

ruscon commented Feb 28, 2019

@chinesedfan, me too, no problems.

I made all the changes, as you can see, is nothing heavy there.
If classes can be used, then I would even more logic here
e8ddb60#diff-c08f94705d80a1dab0bdb72bbdd83daa

It will be something like this

module.exports = class AxiosException extends Error {   
    constructor(error) {
        var json = error.toJSON();
        json.name = AxiosException.name;

        super(JSON.stringify(json, undefined, 2));

        Object.assign(this, error);
    }
}

@AyushG3112
Copy link
Contributor

I think this was sorted with #1415 but a release is pending

@Kunamatata
Copy link

What's the state of having AxiosError be a class as well as an interface in order to be able to use instanceof ?

@sammy007
Copy link

sammy007 commented Feb 1, 2020

OMG. 1 year of jerking with it.

@fubar
Copy link

fubar commented Mar 20, 2020

Is there any progress on this? AxiosError is useless as it is in Typescript.

@Klemensas
Copy link

Having a specific exception would be nice but AxiosError is definitely not useless.
You can write a helper around it, something like:

export function isAxiosError<T>(error: AxiosError | any): error is AxiosError<T> {
  return error && error.isAxiosError
}

And then use it like this:

  catch (exception) {
    if (isAxiosError<apiTypes.ResponseError>(exception) && exception.response) {
       ...
    }
  }

@jasonsaayman
Copy link
Member

Resolved in #2014

@darkbasic
Copy link

@jasonsaayman did it land in any currently released version?

@aeharding
Copy link

@darkbasic I don't think so. I have to add the sha to my package.json:

"axios": "https://github.com/axios/axios.git#58112e137e316c7154e77a3b3777d6788de869b9",

I ran into this issue because Typescript 4.4 defaults catch {} error type to unknown instead of any now, and checking error instanceof AxiosException is the cleanest way to fix it.

@darkbasic
Copy link

Me either.

@CatsMiaow
Copy link

You can apply type guards with the isAxiosError helper function.

isAxiosError(payload: any): payload is AxiosError;

.catch((error) => {
if (axios.isAxiosError(error)) {
const axiosError: AxiosError = error;
}

import axios from 'axios';

if (axios.isAxiosError(error)) {
  // error.response?.data ...
}

@darkbasic
Copy link

That's what I did, but having to write your own type guard sucks :)

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

No branches or pull requests