From 210c71dccd8d8fdeadd9b4d1571e5fdb93e0f02f Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Wed, 23 Jan 2019 12:51:41 -0500 Subject: [PATCH] fix(db_ops): ensure we async resolve errors in createCollection Passing in a name which does not pass collection name validation when a database does not exist will result in throwing an uncatchable error from the driver, resulting in application failure. We need to catch and return these errors asynchronously. NODE-1839 --- lib/operations/db_ops.js | 15 ++++++++++----- test/functional/collection_tests.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/operations/db_ops.js b/lib/operations/db_ops.js index 8cf5dcdd523..27179667d5d 100644 --- a/lib/operations/db_ops.js +++ b/lib/operations/db_ops.js @@ -248,11 +248,16 @@ function createCollection(db, name, options, callback) { // Execute command executeCommand(db, cmd, finalOptions, err => { if (err) return handleCallback(callback, err); - handleCallback( - callback, - null, - new Collection(db, db.s.topology, db.s.databaseName, name, db.s.pkFactory, options) - ); + + try { + return handleCallback( + callback, + null, + new Collection(db, db.s.topology, db.s.databaseName, name, db.s.pkFactory, options) + ); + } catch (err) { + return handleCallback(callback, err); + } }); }); } diff --git a/test/functional/collection_tests.js b/test/functional/collection_tests.js index 8facf4433b9..dd06ac44e65 100644 --- a/test/functional/collection_tests.js +++ b/test/functional/collection_tests.js @@ -497,6 +497,34 @@ describe('Collection', function() { } }); + it('should return invalid collection name error by callback for createCollection', { + metadata: { + requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } + }, + + test: function(done) { + const client = this.configuration.newClient(this.configuration.writeConcernMax(), { + poolSize: 1 + }); + + client.connect((err, client) => { + expect(err).to.not.exist; + + const db = client.db('test_crate_collection'); + + db.dropDatabase(err => { + expect(err).to.not.exist; + + db.createCollection('test/../', err => { + expect(err).to.exist; + client.close(); + done(); + }); + }); + }); + } + }); + /** * @ignore */