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 isAxiosError flag to errors thrown by axios #1419

Merged
merged 4 commits into from Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -81,6 +81,7 @@ export interface AxiosError extends Error {
code?: string;
request?: any;
response?: AxiosResponse;
isAxiosError: boolean;
}

export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
Expand Down
3 changes: 3 additions & 0 deletions lib/core/enhanceError.js
Expand Up @@ -15,8 +15,11 @@ module.exports = function enhanceError(error, config, code, request, response) {
if (code) {
error.code = code;
}

error.request = request;
error.response = response;
error.isAxiosError = true;

error.toJSON = function() {
return {
// Standard
Expand Down
3 changes: 2 additions & 1 deletion test/specs/core/createError.spec.js
@@ -1,7 +1,7 @@
var createError = require('../../../lib/core/createError');

describe('core::createError', function() {
it('should create an Error with message, config, code, request and response', function() {
it('should create an Error with message, config, code, request, response and isAxiosError', function() {
var request = { path: '/foo' };
var response = { status: 200, data: { foo: 'bar' } };
var error = createError('Boom!', { foo: 'bar' }, 'ESOMETHING', request, response);
Expand All @@ -11,6 +11,7 @@ describe('core::createError', function() {
expect(error.code).toBe('ESOMETHING');
expect(error.request).toBe(request);
expect(error.response).toBe(response);
expect(error.isAxiosError).toBe(true);
});
it('should create an Error that can be serialized to JSON', function() {
// Attempting to serialize request and response results in
Expand Down
1 change: 1 addition & 0 deletions test/specs/core/enhanceError.spec.js
Expand Up @@ -11,6 +11,7 @@ describe('core::enhanceError', function() {
expect(error.code).toBe('ESOMETHING');
expect(error.request).toBe(request);
expect(error.response).toBe(response);
expect(error.isAxiosError).toBe(true);
});

it('should return error', function() {
Expand Down