Skip to content

Commit

Permalink
Refactor axios errors creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbsgilbs committed Sep 29, 2019
1 parent 239ddda commit 182b4d5
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ function resetHistory() {
this.history = getVerbObject();
}

function createAxiosError(message, config, code) {
var error = new Error(message);
error.config = config;
error.isAxiosError = true;
if (code !== undefined) {
error.code = code;
}
return error;
}

function MockAdapter(axiosInstance, options) {
reset.call(this);

Expand Down Expand Up @@ -95,38 +105,36 @@ VERBS.concat('any').forEach(function(method) {

networkError: function() {
reply(function(config) {
var error = new Error('Network Error');
error.config = config;
error.isAxiosError = true;
var error = createAxiosError('Network Error', config);
return Promise.reject(error);
});
},

networkErrorOnce: function() {
replyOnce(function(config) {
var error = new Error('Network Error');
error.config = config;
error.isAxiosError = true;
var error = createAxiosError('Network Error', config);
return Promise.reject(error);
});
},

timeout: function() {
reply(function(config) {
var error = new Error('timeout of ' + config.timeout + 'ms exceeded');
error.config = config;
error.code = 'ECONNABORTED';
error.isAxiosError = true;
var error = createAxiosError(
'timeout of ' + config.timeout + 'ms exceeded',
config,
'ECONNABORTED'
);
return Promise.reject(error);
});
},

timeoutOnce: function() {
replyOnce(function(config) {
var error = new Error('timeout of ' + config.timeout + 'ms exceeded');
error.config = config;
error.code = 'ECONNABORTED';
error.isAxiosError = true;
var error = createAxiosError(
'timeout of ' + config.timeout + 'ms exceeded',
config,
'ECONNABORTED'
);
return Promise.reject(error);
});
}
Expand Down

0 comments on commit 182b4d5

Please sign in to comment.