Skip to content

Commit

Permalink
fix(connection): emit error if no promise handler
Browse files Browse the repository at this point in the history
Fix #4135
  • Loading branch information
vkarpov15 committed Jun 23, 2016
1 parent ae884e4 commit 2421730
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,20 @@ Connection.prototype.open = function(host, database, port, options, callback) {

var _this = this;
var Promise = PromiseProvider.get();
return new Promise.ES6(function(resolve, reject) {
_this._open(!!callback, function(error) {
var promise = new Promise.ES6(function(resolve, reject) {
_this._open(true, function(error) {
callback && callback(error);
if (error) {
reject(error);
if (!callback && _this.listeners('error').length) {
if (!callback && !promise.$hasHandler) {
_this.emit('error', error);
}
return;
}
resolve();
});
});
return promise;
};

/**
Expand Down Expand Up @@ -416,19 +417,20 @@ Connection.prototype.openSet = function(uris, database, options, callback) {

var _this = this;
var Promise = PromiseProvider.get();
return new Promise.ES6(function(resolve, reject) {
_this._open(!!callback, function(error) {
var promise = new Promise.ES6(function(resolve, reject) {
_this._open(true, function(error) {
callback && callback(error);
if (error) {
reject(error);
if (!callback && _this.listeners('error').length) {
if (!callback && !promise.$hasHandler) {
_this.emit('error', error);
}
return;
}
resolve();
});
});
return promise;
};

/**
Expand Down Expand Up @@ -475,7 +477,7 @@ Connection.prototype._open = function(emit, callback) {
callback(err);
}
} else {
_this.error(err, callback);
_this.error(err, emit && callback);
}
return;
}
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ MongooseThenable.prototype.then = function(onFulfilled, onRejected) {
'has been called'));
}).then(onFulfilled, onRejected);
}
this.$opPromise.$hasHandler = true;
return this.$opPromise.then(onFulfilled, onRejected);
};

Expand Down

0 comments on commit 2421730

Please sign in to comment.