From 252876c2c0a1976861497a0c315a929a710094ff Mon Sep 17 00:00:00 2001 From: Philip Roberts Date: Fri, 22 Jan 2016 16:39:41 +0000 Subject: [PATCH] Fixing reject not being called on xhr network errors. Fixes #204 --- lib/adapters/xhr.js | 10 ++++++++++ test/specs/requests.spec.js | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/lib/adapters/xhr.js b/lib/adapters/xhr.js index cfb3348..fdd455f 100644 --- a/lib/adapters/xhr.js +++ b/lib/adapters/xhr.js @@ -63,6 +63,16 @@ module.exports = function xhrAdapter(resolve, reject, config) { request = null; }; + // Handle low level network errors + request.onerror = function handleError() { + // Real errors are hidden from us by the browser + // onerror should only fire if it's a network error + reject(new Error('Network Error')); + + // Clean up request + request = null; + }; + // Add xsrf header // This is only done if running in a standard browser environment. // Specifically not if we're in a web worker, or react-native. diff --git a/test/specs/requests.spec.js b/test/specs/requests.spec.js index a78918e..bd05a18 100644 --- a/test/specs/requests.spec.js +++ b/test/specs/requests.spec.js @@ -54,6 +54,29 @@ describe('requests', function () { }, 0); }); + it('should reject on network 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 finish = function () { + expect(resolveSpy).not.toHaveBeenCalled(); + + expect(rejectSpy).toHaveBeenCalledWith(jasmine.any(Error)); + expect(rejectSpy.calls.argsFor(0)[0].message).toEqual('Network Error'); + + done(); + }; + + axios({ + url: 'http://thisisnotaserver' + }) + .then(resolveSpy, rejectSpy) + .then(finish, finish); + }); + it('should make cross domian http request', function (done) { var request, response;