From 75f9b8c5fd3680e6f2bb704b3062544ccae16817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 7 Jul 2016 15:59:51 +0200 Subject: [PATCH 1/2] Adding failing test for adapter errors --- test/specs/requests.spec.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/specs/requests.spec.js b/test/specs/requests.spec.js index 0a81d16..6e97459 100644 --- a/test/specs/requests.spec.js +++ b/test/specs/requests.spec.js @@ -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) { // disable jasmine.Ajax since we're hitting a non-existant server anyway jasmine.Ajax.uninstall(); From 235c19c876c2d3933bf776b396859053b012a7a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 7 Jul 2016 16:01:20 +0200 Subject: [PATCH 2/2] Implementing adapter error enhacement --- lib/core/dispatchRequest.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/core/dispatchRequest.js b/lib/core/dispatchRequest.js index c7af27b..7bbb45d 100644 --- a/lib/core/dispatchRequest.js +++ b/lib/core/dispatchRequest.js @@ -2,6 +2,7 @@ var utils = require('./../utils'); var transformData = require('./transformData'); +var enhanceError = require('./enhanceError'); /** * Dispatch a request to the server using whichever adapter @@ -54,7 +55,7 @@ module.exports = function dispatchRequest(config) { adapter(resolve, reject, config); } } catch (e) { - reject(e); + reject(enhanceError(e, config)); } }).then(function onFulfilled(response) { // Transform response data @@ -67,4 +68,3 @@ module.exports = function dispatchRequest(config) { return response; }); }; -