Permalink
Browse files

client: handle `xhr.open()` errors

Closes #1149.
  • Loading branch information...
1 parent 4f75c80 commit 3ad500ddd939e761a21ef6d9981376361872e4f0 @stephenmathieson stephenmathieson committed Jan 8, 2017
Showing with 19 additions and 4 deletions.
  1. +9 −4 lib/client.js
  2. +10 −0 test/client/request.js
View
@@ -738,10 +738,15 @@ Request.prototype.end = function(fn){
this._setTimeouts();
// initiate request
- if (this.username && this.password) {
- xhr.open(this.method, this.url, true, this.username, this.password);
- } else {
- xhr.open(this.method, this.url, true);
+ try {
+ if (this.username && this.password) {
+ xhr.open(this.method, this.url, true, this.username, this.password);
+ } else {
+ xhr.open(this.method, this.url, true);
+ }
+ } catch (err) {
+ // see #1149
+ return this.callback(err);
}
// CORS
@@ -1,3 +1,4 @@
+
var assert = require('assert');
var request = require('../../');
@@ -281,4 +282,13 @@ it('parse should take precedence over default parse', function(done){
});
});
+it('handles `xhr.open()` errors', function(done){
+ request
+ .get('http://foo\0.com') // throws "Failed to execute 'open' on 'XMLHttpRequest': Invalid URL"
+ .end(function(err, res){
+ assert(err);
+ done();
+ });
+});
+
});

0 comments on commit 3ad500d

Please sign in to comment.