From 274a6afb4f9473bf67727e874e693fc66cb69865 Mon Sep 17 00:00:00 2001 From: johachi <47516968+johachi@users.noreply.github.com> Date: Mon, 26 Apr 2021 08:31:28 +0900 Subject: [PATCH] Adding isAxiosError typeguard documentation --- README.md | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5def9d52b2..a013b2b9a7 100755 --- a/README.md +++ b/README.md @@ -826,10 +826,21 @@ axios depends on a native ES6 Promise implementation to be [supported](http://ca If your environment doesn't support ES6 Promises, you can [polyfill](https://github.com/jakearchibald/es6-promise). ## TypeScript -axios includes [TypeScript](http://typescriptlang.org) definitions. + +axios includes [TypeScript](http://typescriptlang.org) definitions and a type guard for axios errors. + ```typescript -import axios from 'axios'; -axios.get('/user?ID=12345'); +let user: User = null; +try { + const { data } = await axios.get('/user?ID=12345'); + user = data.userDetails; +} catch (error) { + if (axios.isAxiosError(error)) { + handleAxiosError(error); + } else { + handleUnexpectedError(error); + } +} ``` ## Online one-click setup