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
Comments
How about checking |
@chinesedfan |
@ruscon I think you can use type assertion.
|
@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 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 |
@chinesedfan heavy way ? |
@ruscon |
@chinesedfan, me too, no problems. I made all the changes, as you can see, is nothing heavy there. 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);
}
} |
I think this was sorted with #1415 but a release is pending |
What's the state of having |
OMG. 1 year of jerking with it. |
Is there any progress on this? |
Having a specific exception would be nice but 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) {
...
}
} |
Resolved in #2014 |
@jasonsaayman did it land in any currently released version? |
@darkbasic I don't think so. I have to add the sha to my package.json:
I ran into this issue because Typescript 4.4 defaults |
Me either. |
You can apply type guards with the Line 168 in f3ca637
axios/test/typescript/axios.ts Lines 369 to 372 in f3ca637
import axios from 'axios';
if (axios.isAxiosError(error)) {
// error.response?.data ...
} |
That's what I did, but having to write your own type guard sucks :) |
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
The text was updated successfully, but these errors were encountered: