Skip to content

Commit

Permalink
fix(connection): don't throw if .catch() on open() promise
Browse files Browse the repository at this point in the history
Re: #5229
vkarpov15 committed May 10, 2017
1 parent edd0207 commit e74c9d3
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
@@ -327,9 +327,42 @@ Connection.prototype.open = function() {
resolve();
});
});

var _then = promise.then;
promise.then = function(resolve, reject) {
promise.$hasHandler = true;
return _then.call(promise, resolve, reject);
};

return promise;
};

/*!
* ignore
*/

Connection.prototype._openWithoutPromise = function() {
var callback;

try {
callback = this._handleOpenArgs.apply(this, arguments);
} catch (error) {
// No need to do anything
}

var _this = this;
this._open(true, function(error) {
callback && callback(error);
if (error && !callback) {
// Error can be on same tick re: christkv/mongodb-core#157
setImmediate(function() {
_this.emit('error', error);
});
return;
}
});
};

/**
* Helper for `dropDatabase()`.
*
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -188,7 +188,7 @@ Mongoose.prototype.createConnection = function(uri, options) {
(rsOption.replicaSet || rsOption.rs_name)) {
conn.openSet.apply(conn, arguments).catch(function() {});
} else {
conn.open.apply(conn, arguments).catch(function() {});
conn._openWithoutPromise.apply(conn, arguments);
}
}

0 comments on commit e74c9d3

Please sign in to comment.