Skip to content

Commit 210c71d

Browse files
committed
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
1 parent 5ad9fa9 commit 210c71d

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

lib/operations/db_ops.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,16 @@ function createCollection(db, name, options, callback) {
248248
// Execute command
249249
executeCommand(db, cmd, finalOptions, err => {
250250
if (err) return handleCallback(callback, err);
251-
handleCallback(
252-
callback,
253-
null,
254-
new Collection(db, db.s.topology, db.s.databaseName, name, db.s.pkFactory, options)
255-
);
251+
252+
try {
253+
return handleCallback(
254+
callback,
255+
null,
256+
new Collection(db, db.s.topology, db.s.databaseName, name, db.s.pkFactory, options)
257+
);
258+
} catch (err) {
259+
return handleCallback(callback, err);
260+
}
256261
});
257262
});
258263
}

test/functional/collection_tests.js

+28
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,34 @@ describe('Collection', function() {
497497
}
498498
});
499499

500+
it('should return invalid collection name error by callback for createCollection', {
501+
metadata: {
502+
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
503+
},
504+
505+
test: function(done) {
506+
const client = this.configuration.newClient(this.configuration.writeConcernMax(), {
507+
poolSize: 1
508+
});
509+
510+
client.connect((err, client) => {
511+
expect(err).to.not.exist;
512+
513+
const db = client.db('test_crate_collection');
514+
515+
db.dropDatabase(err => {
516+
expect(err).to.not.exist;
517+
518+
db.createCollection('test/../', err => {
519+
expect(err).to.exist;
520+
client.close();
521+
done();
522+
});
523+
});
524+
});
525+
}
526+
});
527+
500528
/**
501529
* @ignore
502530
*/

0 commit comments

Comments
 (0)