提交 8f3a4301 编写于 作者: N Nick Uraltsev 提交者: GitHub

Merge pull request #368 from rubennorte/feature/improve-error-handling

Enhance adapter errors
...@@ -2,6 +2,7 @@ ...@@ -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) { ...@@ -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) { ...@@ -67,4 +68,3 @@ module.exports = function dispatchRequest(config) {
return response; return response;
}); });
}; };
...@@ -36,6 +36,35 @@ describe('requests', function () { ...@@ -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();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册