mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40:40 +03:00
Merge pull request #368 from rubennorte/feature/improve-error-handling
Enhance adapter errors
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
var utils = require('./../utils');
|
var utils = require('./../utils');
|
||||||
var transformData = require('./transformData');
|
var transformData = require('./transformData');
|
||||||
|
var enhanceError = require('./enhanceError');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispatch a request to the server using whichever adapter
|
* Dispatch a request to the server using whichever adapter
|
||||||
@@ -54,7 +55,7 @@ module.exports = function dispatchRequest(config) {
|
|||||||
adapter(resolve, reject, config);
|
adapter(resolve, reject, config);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
reject(e);
|
reject(enhanceError(e, config));
|
||||||
}
|
}
|
||||||
}).then(function onFulfilled(response) {
|
}).then(function onFulfilled(response) {
|
||||||
// Transform response data
|
// Transform response data
|
||||||
@@ -67,4 +68,3 @@ module.exports = function dispatchRequest(config) {
|
|||||||
return response;
|
return response;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,35 @@ describe('requests', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should reject on adapter errors', function (done) {
|
||||||
|
// disable jasmine.Ajax since we're hitting a non-existant server anyway
|
||||||
|
jasmine.Ajax.uninstall();
|
||||||
|
|
||||||
|
var resolveSpy = jasmine.createSpy('resolve');
|
||||||
|
var rejectSpy = jasmine.createSpy('reject');
|
||||||
|
|
||||||
|
var adapterError = new Error('adapter error');
|
||||||
|
var adapterThatFails = function () {
|
||||||
|
throw adapterError;
|
||||||
|
};
|
||||||
|
|
||||||
|
var finish = function () {
|
||||||
|
expect(resolveSpy).not.toHaveBeenCalled();
|
||||||
|
expect(rejectSpy).toHaveBeenCalled();
|
||||||
|
var reason = rejectSpy.calls.first().args[0];
|
||||||
|
expect(reason).toBe(adapterError);
|
||||||
|
expect(reason.config.method).toBe('get');
|
||||||
|
expect(reason.config.url).toBe('/foo');
|
||||||
|
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
|
||||||
|
axios('/foo', {
|
||||||
|
adapter: adapterThatFails
|
||||||
|
}).then(resolveSpy, rejectSpy)
|
||||||
|
.then(finish, finish);
|
||||||
|
});
|
||||||
|
|
||||||
it('should reject on network errors', function (done) {
|
it('should reject on network errors', function (done) {
|
||||||
// disable jasmine.Ajax since we're hitting a non-existant server anyway
|
// disable jasmine.Ajax since we're hitting a non-existant server anyway
|
||||||
jasmine.Ajax.uninstall();
|
jasmine.Ajax.uninstall();
|
||||||
|
|||||||
Reference in New Issue
Block a user