Skip to content

Commit 45bc722

Browse files
author
Jessica Lord
authored
fix(db): only callback with MongoError NODE-1293 (#1652)
1 parent a45eb0b commit 45bc722

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

lib/db.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,7 @@ Db.prototype.collection = function(name, options, callback) {
445445
if (callback) callback(null, collection);
446446
return collection;
447447
} catch (err) {
448-
// if(err instanceof MongoError && callback) return callback(err);
449-
if (callback) return callback(err);
448+
if (err instanceof MongoError && callback) return callback(err);
450449
throw err;
451450
}
452451
}

test/functional/db_tests.js

+34
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,40 @@ describe('Db', function() {
110110
}
111111
});
112112

113+
/**
114+
* @ignore
115+
*/
116+
it('should callback with an error only when a MongoError', {
117+
metadata: {
118+
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
119+
},
120+
121+
test: function(done) {
122+
let configuration = this.configuration;
123+
let client = configuration.newClient(configuration.writeConcernMax(), {
124+
poolSize: 1,
125+
auto_reconnect: true
126+
});
127+
128+
client.connect(function(err, client) {
129+
let callbackCalled = 0;
130+
test.equal(null, err);
131+
let db = client.db(configuration.db);
132+
133+
try {
134+
db.collection('collectionCallbackTest', function(e) {
135+
callbackCalled++;
136+
test.equal(null, e);
137+
throw new Error('Erroring on purpose with a non MongoError');
138+
});
139+
} catch (e) {
140+
test.equal(callbackCalled, 1);
141+
done();
142+
}
143+
});
144+
}
145+
});
146+
113147
/**
114148
* @ignore
115149
*/

test/functional/index_tests.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,8 @@ describe('Indexes', function() {
11231123
db.collection('nonexisting', { strict: true }, function(err) {
11241124
test.ok(err != null);
11251125
db.collection('nonexisting', { strict: false }, function(err) {
1126-
test.ok(err != null);
1126+
// When set to false (default) it should not create an error
1127+
test.ok(err === null);
11271128
done();
11281129
});
11291130
});

0 commit comments

Comments
 (0)