mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
Improve error handling
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
var createError = require('../../../lib/core/createError');
|
||||
|
||||
describe('core::createError', function() {
|
||||
it('should create an Error with message, config, and code', function() {
|
||||
var error = createError('Boom!', { foo: 'bar' }, 'ESOMETHING');
|
||||
expect(error instanceof Error).toBe(true);
|
||||
expect(error.message).toBe('Boom!');
|
||||
expect(error.config).toEqual({ foo: 'bar' });
|
||||
expect(error.code).toBe('ESOMETHING');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
var enhanceError = require('../../../lib/core/enhanceError');
|
||||
|
||||
describe('core::enhanceError', function() {
|
||||
it('should add config and code to error', function() {
|
||||
var error = new Error('Boom!');
|
||||
enhanceError(error, { foo: 'bar' }, 'ESOMETHING');
|
||||
expect(error.config).toEqual({ foo: 'bar' });
|
||||
expect(error.code).toBe('ESOMETHING');
|
||||
});
|
||||
|
||||
it('should return error', function() {
|
||||
var error = new Error('Boom!');
|
||||
expect(enhanceError(error, { foo: 'bar' }, 'ESOMETHING')).toBe(error);
|
||||
});
|
||||
});
|
||||
@@ -58,7 +58,12 @@ describe('core::settle', function() {
|
||||
};
|
||||
settle(resolve, reject, response);
|
||||
expect(resolve).not.toHaveBeenCalled();
|
||||
expect(reject).toHaveBeenCalledWith(response);
|
||||
expect(reject).toHaveBeenCalled();
|
||||
var reason = reject.calls.first().args[0];
|
||||
expect(reason instanceof Error).toBe(true);
|
||||
expect(reason.message).toBe('Request failed with status code 500');
|
||||
expect(reason.config).toBe(response.config);
|
||||
expect(reason.response).toBe(response);
|
||||
});
|
||||
|
||||
it('should pass status to validateStatus', function() {
|
||||
|
||||
@@ -46,6 +46,10 @@ describe('requests', function () {
|
||||
var finish = function () {
|
||||
expect(resolveSpy).not.toHaveBeenCalled();
|
||||
expect(rejectSpy).toHaveBeenCalled();
|
||||
var reason = rejectSpy.calls.first().args[0];
|
||||
expect(reason instanceof Error).toBe(true);
|
||||
expect(reason.config.method).toBe('get');
|
||||
expect(reason.config.url).toBe('http://thisisnotaserver');
|
||||
|
||||
done();
|
||||
};
|
||||
@@ -68,6 +72,13 @@ describe('requests', function () {
|
||||
.then(function () {
|
||||
expect(resolveSpy).not.toHaveBeenCalled();
|
||||
expect(rejectSpy).toHaveBeenCalled();
|
||||
var reason = rejectSpy.calls.first().args[0];
|
||||
expect(reason instanceof Error).toBe(true);
|
||||
expect(reason.message).toBe('Request failed with status code 500');
|
||||
expect(reason.config.method).toBe('get');
|
||||
expect(reason.config.url).toBe('/foo');
|
||||
expect(reason.response.status).toBe(500);
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
|
||||
@@ -25,8 +25,8 @@ module.exports = {
|
||||
timeout: 250
|
||||
}).then(function (res) {
|
||||
success = true;
|
||||
}).catch(function (res) {
|
||||
error = res;
|
||||
}).catch(function (err) {
|
||||
error = err;
|
||||
failure = true;
|
||||
});
|
||||
|
||||
@@ -189,8 +189,8 @@ module.exports = {
|
||||
maxContentLength: 2000
|
||||
}).then(function (res) {
|
||||
success = true;
|
||||
}).catch(function (res) {
|
||||
error = res;
|
||||
}).catch(function (err) {
|
||||
error = err;
|
||||
failure = true;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user