Skip to content

Commit

Permalink
Adding isAxiosError flag to errors thrown by axios (#1419)
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushG3112 authored and Khaledgarbaya committed Aug 20, 2018
1 parent c0b4065 commit b681e91
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 1 deletion.
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

0 comments on commit b681e91

Please sign in to comment.