Skip to content

Commit

Permalink
No longer catch exceptions when on('error') #1100
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Oct 22, 2013
1 parent 2892d3a commit f2d1881
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
4 changes: 4 additions & 0 deletions HISTORY
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.4
-----------------
- All node exceptions will no longer be caught if on('error') is defined

1.3.19 2013-08-21
-----------------
- Correctly rethrowing errors after change from event emission to callbacks, compatibility with 0.10.X domains without breaking 0.8.X support.
Expand Down
16 changes: 9 additions & 7 deletions lib/mongodb/connection/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,16 @@ DbStore.prototype.fetch = function(databaseName) {
DbStore.prototype.emit = function(event, message, object, reset, filterDb, rethrow_if_no_listeners) {
var emitted = false;

// Not emitted and we have enabled rethrow, let process.uncaughtException
// deal with the issue
if(!emitted && rethrow_if_no_listeners) {
return process.nextTick(function() {
throw message;
})
}

// Emit the events
for(var i = 0; i < this._dbs.length; i++) {
for(var i = 0; i < this._dbs.length; i++) {
if(this._dbs[i].listeners(event).length > 0) {
if(filterDb == null || filterDb.databaseName !== this._dbs[i].databaseName
|| filterDb.tag !== this._dbs[i].tag) {
Expand All @@ -217,12 +225,6 @@ DbStore.prototype.emit = function(event, message, object, reset, filterDb, rethr
object.db.emit(event, message, null);
})
}

// Not emitted and we have enabled rethrow, let process.uncaughtException
// deal with the issue
if(!emitted && rethrow_if_no_listeners) {
throw message;
}
}

var Base = function Base() {
Expand Down
23 changes: 11 additions & 12 deletions test/tests/functional/error_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,14 @@ exports['mixing included and excluded fields should return an error object with
}

exports['should handle error throw in user callback'] = function(configuration, test) {
var client = configuration.newDbInstance({w:1}, {poolSize:1});
client.on('error', function(err) {
test.ok(err != null);
client.close();
var db = configuration.newDbInstance({w:1}, {poolSize:1});
process.once("uncaughtException", function(err) {
db.close();
test.done();
})

client.open(function(err, client) {
var c = client.collection('test_error_object_should_include_message');
db.open(function(err, client) {
var c = db.collection('test_error_object_should_include_message');
c.findOne({}, function() {
ggg
})
Expand All @@ -274,10 +273,10 @@ exports['Should handle uncaught error correctly'] = function(configuration, test
exports['Should handle throw error in db operation correctly'] = function(configuration, test) {
var db = configuration.newDbInstance({w:1}, {poolSize:1});
db.open(function(err, db) {
db.on("error", function(err) {
process.once("uncaughtException", function(err) {
db.close();
test.done();
});
test.done();
})

db.collection('t').findOne(function() {
testdfdma();
Expand Down Expand Up @@ -311,10 +310,10 @@ exports['Should handle MongoClient uncaught error correctly'] = {
exports['Should handle MongoClient throw error in db operation correctly'] = function(configuration, test) {
var MongoClient = configuration.getMongoPackage().MongoClient;
MongoClient.connect(configuration.url(), function(err, db) {
db.on("error", function(err) {
process.once("uncaughtException", function(err) {
db.close();
test.done();
});
test.done();
})

db.collection('t').findOne(function() {
testdfdma();
Expand Down
6 changes: 4 additions & 2 deletions test/tests/repl_set/connecting_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,14 @@ exports['Should emit close with callback'] = function(configuration, test) {
new Db('integration_test_', replSet, {w:0}).open(function(err, db) {
test.equal(null, err);
var dbCloseCount = 0;
db.on('close', function() { ++dbCloseCount; });
db.on('close', function() {
++dbCloseCount;
});

db.close(function() {
// Let all events fire.
process.nextTick(function() {
test.equal(dbCloseCount, 0);
test.equal(dbCloseCount, 1);
test.done();
});
});
Expand Down
15 changes: 5 additions & 10 deletions test/tests/repl_set/secondary_queries_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,11 @@ exports['Should fail to do map reduce to out collection'] = function(configurati
// Reduce function
var reduce = function(k,vals) { return 1; };

try {
// Execute map reduce and return results inline
collection.mapReduce(map, reduce
, {out : {replace:'replacethiscollection'}, readPreference:ReadPreference.SECONDARY}, function(err, results) {
});

test.ok(false);
} catch (err) {
test.done();
}
// Execute map reduce and return results inline
collection.mapReduce(map, reduce
, {out : {replace:'replacethiscollection'}, readPreference:ReadPreference.SECONDARY}, function(err, results) {
test.done();
});
} else {
test.done();
}
Expand Down

0 comments on commit f2d1881

Please sign in to comment.