mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Adding isAxiosError flag to errors thrown by axios (#1419)
This commit is contained in:
committed by
Khaled Garbaya
parent
c0b40650d1
commit
b681e919c4
Vendored
+1
@@ -81,6 +81,7 @@ export interface AxiosError extends Error {
|
|||||||
code?: string;
|
code?: string;
|
||||||
request?: any;
|
request?: any;
|
||||||
response?: AxiosResponse;
|
response?: AxiosResponse;
|
||||||
|
isAxiosError: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
|
export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
|
||||||
|
|||||||
@@ -15,8 +15,11 @@ module.exports = function enhanceError(error, config, code, request, response) {
|
|||||||
if (code) {
|
if (code) {
|
||||||
error.code = code;
|
error.code = code;
|
||||||
}
|
}
|
||||||
|
|
||||||
error.request = request;
|
error.request = request;
|
||||||
error.response = response;
|
error.response = response;
|
||||||
|
error.isAxiosError = true;
|
||||||
|
|
||||||
error.toJSON = function() {
|
error.toJSON = function() {
|
||||||
return {
|
return {
|
||||||
// Standard
|
// Standard
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
var createError = require('../../../lib/core/createError');
|
var createError = require('../../../lib/core/createError');
|
||||||
|
|
||||||
describe('core::createError', function() {
|
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 request = { path: '/foo' };
|
||||||
var response = { status: 200, data: { foo: 'bar' } };
|
var response = { status: 200, data: { foo: 'bar' } };
|
||||||
var error = createError('Boom!', { foo: 'bar' }, 'ESOMETHING', request, response);
|
var error = createError('Boom!', { foo: 'bar' }, 'ESOMETHING', request, response);
|
||||||
@@ -11,6 +11,7 @@ describe('core::createError', function() {
|
|||||||
expect(error.code).toBe('ESOMETHING');
|
expect(error.code).toBe('ESOMETHING');
|
||||||
expect(error.request).toBe(request);
|
expect(error.request).toBe(request);
|
||||||
expect(error.response).toBe(response);
|
expect(error.response).toBe(response);
|
||||||
|
expect(error.isAxiosError).toBe(true);
|
||||||
});
|
});
|
||||||
it('should create an Error that can be serialized to JSON', function() {
|
it('should create an Error that can be serialized to JSON', function() {
|
||||||
// Attempting to serialize request and response results in
|
// Attempting to serialize request and response results in
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ describe('core::enhanceError', function() {
|
|||||||
expect(error.code).toBe('ESOMETHING');
|
expect(error.code).toBe('ESOMETHING');
|
||||||
expect(error.request).toBe(request);
|
expect(error.request).toBe(request);
|
||||||
expect(error.response).toBe(response);
|
expect(error.response).toBe(response);
|
||||||
|
expect(error.isAxiosError).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return error', function() {
|
it('should return error', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user