Skip to content

Commit 94fc4ea

Browse files
Adding isAxiosError typeguard documentation (#3767)
Co-authored-by: Jay <jasonsaayman@gmail.com>
1 parent 0ece97c commit 94fc4ea

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -826,10 +826,21 @@ axios depends on a native ES6 Promise implementation to be [supported](http://ca
826826
If your environment doesn't support ES6 Promises, you can [polyfill](https://github.com/jakearchibald/es6-promise).
827827

828828
## TypeScript
829-
axios includes [TypeScript](http://typescriptlang.org) definitions.
829+
830+
axios includes [TypeScript](http://typescriptlang.org) definitions and a type guard for axios errors.
831+
830832
```typescript
831-
import axios from 'axios';
832-
axios.get('/user?ID=12345');
833+
let user: User = null;
834+
try {
835+
const { data } = await axios.get('/user?ID=12345');
836+
user = data.userDetails;
837+
} catch (error) {
838+
if (axios.isAxiosError(error)) {
839+
handleAxiosError(error);
840+
} else {
841+
handleUnexpectedError(error);
842+
}
843+
}
833844
```
834845

835846
## Online one-click setup

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy