Skip to content

Commit

Permalink
fix(db_ops): ensure we async resolve errors in createCollection
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mbroadst committed Jan 23, 2019
1 parent 5ad9fa9 commit 210c71d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/operations/db_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
});
}
Expand Down
28 changes: 28 additions & 0 deletions test/functional/collection_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 210c71d

Please sign in to comment.