Skip to content

Commit

Permalink
Merge pull request #205 from latentflip/fix-204
Browse files Browse the repository at this point in the history
Fixing reject not being called on xhr network errors.
  • Loading branch information
mzabriskie committed Jan 22, 2016
2 parents a473744 + 252876c commit bee0b28
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/adapters/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions test/specs/requests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit bee0b28

Please sign in to comment.