Skip to content

Commit f472e5d

Browse files
authored
Adding a type guard for AxiosError (#2949)
Co-authored-by: Jason Kwok <JasonHK@users.noreply.github.com>
1 parent 7688255 commit f472e5d

File tree

7 files changed

+49
-0
lines changed

7 files changed

+49
-0
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export interface AxiosStatic extends AxiosInstance {
153153
isCancel(value: any): boolean;
154154
all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
155155
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
156+
isAxiosError(payload: any): payload is AxiosError;
156157
}
157158

158159
declare const axios: AxiosStatic;

lib/axios.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ axios.all = function all(promises) {
4747
};
4848
axios.spread = require('./helpers/spread');
4949

50+
// Expose isAxiosError
51+
axios.isAxiosError = require('./helpers/isAxiosError');
52+
5053
module.exports = axios;
5154

5255
// Allow use of default import syntax in TypeScript

lib/helpers/isAxiosError.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
/**
4+
* Determines whether the payload is an error thrown by Axios
5+
*
6+
* @param {*} payload The value to test
7+
* @returns {boolean} True if the payload is an error thrown by Axios, otherwise false
8+
*/
9+
module.exports = function isAxiosError(payload) {
10+
return (typeof payload === 'object') && (payload.isAxiosError === true);
11+
};

test/specs/api.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ describe('static api', function () {
4141
expect(typeof axios.CancelToken).toEqual('function');
4242
expect(typeof axios.isCancel).toEqual('function');
4343
});
44+
45+
it('should have isAxiosError properties', function () {
46+
expect(typeof axios.isAxiosError).toEqual('function');
47+
});
4448
});
4549

4650
describe('instance api', function () {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var createError = require('../../../lib/core/createError');
2+
var enhanceError = require('../../../lib/core/enhanceError');
3+
var isAxiosError = require('../../../lib/helpers/isAxiosError');
4+
5+
describe('helpers::isAxiosError', function () {
6+
it('should return true if the error is created by core::createError', function () {
7+
expect(isAxiosError(createError('Boom!', { foo: 'bar' })))
8+
.toBe(true);
9+
});
10+
11+
it('should return true if the error is enhanced by core::enhanceError', function () {
12+
expect(isAxiosError(enhanceError(new Error('Boom!'), { foo: 'bar' })))
13+
.toBe(true);
14+
});
15+
16+
it('should return false if the error is a normal Error instance', function () {
17+
expect(isAxiosError(new Error('Boom!')))
18+
.toBe(false);
19+
});
20+
});

test/specs/instance.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('instance', function () {
1919
'isCancel',
2020
'all',
2121
'spread',
22+
'isAxiosError',
2223
'default'].indexOf(prop) > -1) {
2324
continue;
2425
}

test/typescript/axios.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,12 @@ axios.get('/user', {
358358
});
359359

360360
source.cancel('Operation has been canceled.');
361+
362+
// AxiosError
363+
364+
axios.get('/user')
365+
.catch((error) => {
366+
if (axios.isAxiosError(error)) {
367+
const axiosError: AxiosError = error;
368+
}
369+
});

0 commit comments

Comments
 (0)