Skip to content

Commit 94fc4ea

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

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

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)