axios: How to type axios error in Typescript?
Describe the issue
I have question how type axios error in Typescript.
I know AxiosError
type is exposed, but I don’t think it should be used because it’s unclear if it’s an Axios Error when caught.
Example Code
import axios, {AxiosError} from 'axios';
axios.get('v1/users')
.catch((e: AxiosError) => { // really AxiosError?
console.log(e.message);
}
Should I do something judgement before typing with AxiosError? Please tell me a good practice.
Expected behavior, if applicable
A clear and concise description of what you expected to happen.
Environment
- Axios Version [e.g. 0.18.0]
- Adapter [e.g. XHR/HTTP]
- Browser [e.g. Chrome, Safari]
- Browser Version [e.g. 22]
- Node.js Version [e.g. 13.0.1]
- OS: [e.g. iOS 12.1.0, OSX 10.13.4]
- Additional Library Versions [e.g. React 16.7, React Native 0.58.0]
Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 54
- Comments: 18 (3 by maintainers)
@EkeMinusYou This is a great question. If you look at the types you’ll see that
AxiosError
has a propertyisAxiosError
that is used to detect types, when combined with the builtin typeguard:This is a newer feature, so I suggest you update to the newest version of Axios to ensure it is present.
I have a scenario where the error response is different than the successful response and I’m trying to figure out what’s the best way to type it. So far I came up with this but not sure it’s the best thing to do, I would appreciate your feedback 😄 https://codesandbox.io/s/upbeat-easley-ljgir?file=/src/index.ts
just in case this helps anyone else I created some middleware based on this answer to make the code a bit cleaner in the eventual request:
Then the code looks like:
It worked for me:
axios.isAxiosError
is useful, but it doesn’t type-guard or allow you to specify a generic response type. I added a really simple util function in my codebase to help with those:You can then use it to get fully-typed error response data:
Error
is defined by the typescript libs for your target (in your tsconfig.json file). For example: https://github.com/microsoft/TypeScript/blob/main/lib/lib.es5.d.ts#L1052Revisiting this after a year for learning more about ts/js I would rewrite it as:
Assuming errors are of created by
new Error(...)
in javascript is a bad practice. You can technically throw anything.throw "a string";
is valid, unfortunately.The browser and node.js can throw native errors that don’t extend the
Error
class. For example, GeolocationPositionError is not anError
as it may not contain a stack trace.my error interface.
my catch error.
To identify an
AxiosError
in thecatch
clause, the isAxiosError function can be used. I made a video tutorial about it: https://www.youtube.com/watch?v=NGSck4aHfeQI am having a little problem understanding this. MyExpectedResponseType is the expected data type to get back from a successful response? Why do we want to access it in a failed response. I thought response.data would hold information on why it failed. For example data submitted to request was incorrect and it responds with which data field failed. That would be a different type to say the data type of a successful response
I would love something more concise, like
axios.get<ResponseDataType, ErrorType>
to allow types to just flow through?Looks like i need to upgrade axios package
Hi there @timemachine3030, What is the
Error
type? A type you created?I’d guess that this is useful in the case of known/expected types of Axios errors. For example, I use the following to provide type info to Laravel validation error responses (always status code
422
withmessage
&errors
data properties):While I probably wouldn’t go with @btraut’s approach as it doesn’t allow for differentiating between different types of errors, it could be useful as a quick type cast.
` import axios, { AxiosError } from “axios”
try {
`