Skip to content

Commit

Permalink
(pouchdb#4595) - Catch xhr errors and propagate
Browse files Browse the repository at this point in the history
- Reject Promise
- Emit error event
  • Loading branch information
daedlock committed Dec 30, 2015
1 parent 78db7d2 commit 7536e68
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/deps/ajax/request-browser.js
Expand Up @@ -120,7 +120,12 @@ function xhRequest(options, callback) {
xhr = new XMLHttpRequest();
}

xhr.open(options.method, options.url);
try {
xhr.open(options.method, options.url);
}
catch(exception) {
callback(exception,{x:'5',statusCode:413});
}

xhr.withCredentials = ('withCredentials' in options) ?
options.withCredentials : true;
Expand Down
6 changes: 6 additions & 0 deletions lib/deps/errors.js
Expand Up @@ -157,6 +157,12 @@ exports.MISSING_STUB = new PouchError({
error: 'missing_stub'
});

exports.INVALID_URL = new PouchError({
status: 413,
error: 'invalid_url',
reason: 'Provided URL is invalid'
});

exports.error = function (error, reason, name) {
function CustomPouchError(reason) {
// inherit error properties from our parent error manually
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/test.basics.js
Expand Up @@ -83,6 +83,22 @@ adapters.forEach(function (adapter) {
});
});

it('[4595] should reject xhr errors', function(done){
var invalidUrl = 'http:///';
new PouchDB(dbs.name).replicate.to(invalidUrl,{})
.catch(function(err,changes){
done();
});

});
it('[4595] should emit error event on xhr error', function(done){
var invalidUrl = 'http:///';
new PouchDB(dbs.name).replicate.to(invalidUrl,{})
.on('error',function(err,changes){
done();
});
});

it('Add a doc', function (done) {
var db = new PouchDB(dbs.name);
db.post({test: 'somestuff'}, function (err, info) {
Expand Down

0 comments on commit 7536e68

Please sign in to comment.