In the axios document, they used this piece of code
try {
const { data } = await axios.get('/user?ID=12345');
user = data.userDetails;
} catch (error) {
if (axios.isAxiosError(error)) {
handleAxiosError(error);
} else {
handleUnexpectedError(error);
}
}
And if I use axios, in the error I can also see isAxiosError: true. But all the error has this same property.
Of course, if I do this. Then there will be no isAxiosError field at all.
axiosInstance.interceptors.response.use(
(response) => response,
(err) => {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
return Promise.reject(new Error("error");
}
);
My question is: is there any situation that has isAxiosError: false
All successfully executed calls using axios, when failing, will be identified as an axios error.
But in you example, if the request is correct, but the returned value data is undefined, you will receive cannot read property userDetails of undefined, which would not be an axios error.
If you wrap only the axios request, you can assume that you will receive an axios error. But if you include other logic, that logic would be able to throw errors not related to axios.
So it can be used to separate the errors generated by axios with other errors that could be thrown within the same try/catch.
EDIT:
Thinking about it, you probably wonder if isAxiosError ever is defined but set to false?
I would guess no. As the property has ta be defined to be read/found, a boolean makes the most sense.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With