Skip to content

Commit a3adb3f

Browse files
authored
fix(bulk): fix error propagation in empty bulk.execute
Executing an empty bulk write is supposed to return a detailed MongoError, but we were not properly returning the error object, resulting in a cryptic error. Fixes NODE-1822
1 parent ec0e30e commit a3adb3f

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

lib/bulk/ordered.js

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const executeOperation = utils.executeOperation;
1212
const MongoWriteConcernError = require('mongodb-core').MongoWriteConcernError;
1313
const handleMongoWriteConcernError = require('./common').handleMongoWriteConcernError;
1414
const bson = common.bson;
15+
const isPromiseLike = require('../utils').isPromiseLike;
1516

1617
/**
1718
* Add to internal list of Operations
@@ -114,6 +115,10 @@ class OrderedBulkOperation extends BulkOperationBase {
114115
*/
115116
execute(_writeConcern, options, callback) {
116117
const ret = this.bulkExecute(_writeConcern, options, callback);
118+
if (isPromiseLike(ret)) {
119+
return ret;
120+
}
121+
117122
options = ret.options;
118123
callback = ret.callback;
119124

lib/bulk/unordered.js

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const executeOperation = utils.executeOperation;
1212
const MongoWriteConcernError = require('mongodb-core').MongoWriteConcernError;
1313
const handleMongoWriteConcernError = require('./common').handleMongoWriteConcernError;
1414
const bson = common.bson;
15+
const isPromiseLike = require('../utils').isPromiseLike;
1516

1617
/**
1718
* Add to internal list of Operations
@@ -126,6 +127,10 @@ class UnorderedBulkOperation extends BulkOperationBase {
126127
*/
127128
execute(_writeConcern, options, callback) {
128129
const ret = this.bulkExecute(_writeConcern, options, callback);
130+
if (isPromiseLike(ret)) {
131+
return ret;
132+
}
133+
129134
options = ret.options;
130135
callback = ret.callback;
131136

test/functional/bulk_tests.js

+36
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const test = require('./shared').assert,
33
setupDatabase = require('./shared').setupDatabase,
44
expect = require('chai').expect;
55

6+
const MongoError = require('../../index').MongoError;
7+
68
describe('Bulk', function() {
79
before(function() {
810
return setupDatabase(this.configuration);
@@ -1571,4 +1573,38 @@ describe('Bulk', function() {
15711573
});
15721574
});
15731575
});
1576+
1577+
function testPropagationOfBulkWriteError(bulk) {
1578+
return bulk.execute().then(
1579+
err => {
1580+
expect(err).to.be.an.instanceOf(MongoError);
1581+
},
1582+
err => {
1583+
expect(err).to.be.an.instanceOf(MongoError);
1584+
expect(err).to.not.be.an.instanceOf(TypeError);
1585+
expect(err.driver).to.equal(true);
1586+
expect(err.name).to.equal('MongoError');
1587+
}
1588+
);
1589+
}
1590+
1591+
it('should propagate the proper error from executing an empty ordered batch', function() {
1592+
const client = this.configuration.newClient();
1593+
1594+
return client.connect().then(() => {
1595+
const collection = client.db(this.configuration.db).collection('doesnt_matter');
1596+
1597+
return testPropagationOfBulkWriteError(collection.initializeOrderedBulkOp());
1598+
});
1599+
});
1600+
1601+
it('should propagate the proper error from executing an empty unordered batch', function() {
1602+
const client = this.configuration.newClient();
1603+
1604+
return client.connect().then(() => {
1605+
const collection = client.db(this.configuration.db).collection('doesnt_matter');
1606+
1607+
return testPropagationOfBulkWriteError(collection.initializeUnorderedBulkOp());
1608+
});
1609+
});
15741610
});

0 commit comments

Comments
 (0)