From 603e7c18fda47ce4bf629377f256dd77fdd090f2 Mon Sep 17 00:00:00 2001 From: Jessica Lord Date: Thu, 25 Jan 2018 19:21:56 -0500 Subject: [PATCH 1/3] fix(db): only callback with MongoError --- lib/db.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/db.js b/lib/db.js index 70fa11395fb..4b3e6a0d0d1 100644 --- a/lib/db.js +++ b/lib/db.js @@ -445,8 +445,7 @@ Db.prototype.collection = function(name, options, callback) { if (callback) callback(null, collection); return collection; } catch (err) { - // if(err instanceof MongoError && callback) return callback(err); - if (callback) return callback(err); + if (err instanceof MongoError && callback) return callback(err); throw err; } } From 7a8dab0cd1beff7292cf262557ee3a46c1f9e2e1 Mon Sep 17 00:00:00 2001 From: Jessica Lord Date: Fri, 26 Jan 2018 11:24:23 -0500 Subject: [PATCH 2/3] fix(indexes): should not create an error when strict is false --- test/functional/index_tests.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/functional/index_tests.js b/test/functional/index_tests.js index 43a0a64958e..6ad31e13ce0 100644 --- a/test/functional/index_tests.js +++ b/test/functional/index_tests.js @@ -1123,7 +1123,8 @@ describe('Indexes', function() { db.collection('nonexisting', { strict: true }, function(err) { test.ok(err != null); db.collection('nonexisting', { strict: false }, function(err) { - test.ok(err != null); + // When set to false (default) it should not create an error + test.ok(err === null); done(); }); }); From 38069a6170e1884f7af92b42fea1b5e4d407abe8 Mon Sep 17 00:00:00 2001 From: Jessica Lord Date: Fri, 26 Jan 2018 11:33:17 -0500 Subject: [PATCH 3/3] test(db): check callback is called only with MongoError --- test/functional/db_tests.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/functional/db_tests.js b/test/functional/db_tests.js index 7a712bbb804..e2f967801a6 100644 --- a/test/functional/db_tests.js +++ b/test/functional/db_tests.js @@ -110,6 +110,40 @@ describe('Db', function() { } }); + /** + * @ignore + */ + it('should callback with an error only when a MongoError', { + metadata: { + requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } + }, + + test: function(done) { + let configuration = this.configuration; + let client = configuration.newClient(configuration.writeConcernMax(), { + poolSize: 1, + auto_reconnect: true + }); + + client.connect(function(err, client) { + let callbackCalled = 0; + test.equal(null, err); + let db = client.db(configuration.db); + + try { + db.collection('collectionCallbackTest', function(e) { + callbackCalled++; + test.equal(null, e); + throw new Error('Erroring on purpose with a non MongoError'); + }); + } catch (e) { + test.equal(callbackCalled, 1); + done(); + } + }); + } + }); + /** * @ignore */