Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a type guard for AxiosError #2949

Merged
merged 13 commits into from Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -153,6 +153,7 @@ export interface AxiosStatic extends AxiosInstance {
isCancel(value: any): boolean;
all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
isAxiosError(payload: any): payload is AxiosError;
}

declare const Axios: AxiosStatic;
Expand Down
3 changes: 3 additions & 0 deletions lib/axios.js
Expand Up @@ -47,6 +47,9 @@ axios.all = function all(promises) {
};
axios.spread = require('./helpers/spread');

// Expose isAxiosError
axios.isAxiosError = require('./helpers/isAxiosError');

module.exports = axios;

// Allow use of default import syntax in TypeScript
Expand Down
11 changes: 11 additions & 0 deletions lib/helpers/isAxiosError.js
@@ -0,0 +1,11 @@
'use strict';

/**
* Determines whether the payload is an error thrown by Axios
*
* @param {*} payload The value to test
* @returns {boolean} True if the payload is an error thrown by Axios, otherwise false
*/
module.exports = function isAxiosError(payload) {
return (typeof payload === 'object') && (payload.isAxiosError === true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeof null === 'object' => true

Should be changed to:

({}).toString.call(payload) === '[obejct Object]'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to make a strict inspection here? @jasonsaayman @JasonHK

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I totally forgot about this. But because this PR had already been merged, I will open another PR to fix this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK~

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have submitted the relevant PR: #3546

};
4 changes: 4 additions & 0 deletions test/specs/api.spec.js
Expand Up @@ -41,6 +41,10 @@ describe('static api', function () {
expect(typeof axios.CancelToken).toEqual('function');
expect(typeof axios.isCancel).toEqual('function');
});

it('should have isAxiosError properties', function () {
expect(typeof axios.isAxiosError).toEqual('function');
});
});

describe('instance api', function () {
Expand Down
20 changes: 20 additions & 0 deletions test/specs/helpers/isAxiosError.spec.js
@@ -0,0 +1,20 @@
var createError = require('../../../lib/core/createError');
var enhanceError = require('../../../lib/core/enhanceError');
var isAxiosError = require('../../../lib/helpers/isAxiosError');

describe('helpers::isAxiosError', function () {
it('should return true if the error is created by core::createError', function () {
expect(isAxiosError(createError('Boom!', { foo: 'bar' })))
.toBe(true);
});

it('should return true if the error is enhanced by core::enhanceError', function () {
expect(isAxiosError(enhanceError(new Error('Boom!'), { foo: 'bar' })))
.toBe(true);
});

it('should return false if the error is a normal Error instance', function () {
expect(isAxiosError(new Error('Boom!')))
.toBe(false);
});
});
1 change: 1 addition & 0 deletions test/specs/instance.spec.js
Expand Up @@ -19,6 +19,7 @@ describe('instance', function () {
'isCancel',
'all',
'spread',
'isAxiosError',
'default'].indexOf(prop) > -1) {
continue;
}
Expand Down
9 changes: 9 additions & 0 deletions test/typescript/axios.ts
Expand Up @@ -350,3 +350,12 @@ axios.get('/user', {
});

source.cancel('Operation has been canceled.');

// AxiosError

axios.get('/user')
.catch((error) => {
if (axios.isAxiosError(error)) {
const axiosError: AxiosError = error;
}
});