Skip to content

Commit

Permalink
fix(bulk): fix error propagation in empty bulk.execute
Browse files Browse the repository at this point in the history
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
  • Loading branch information
daprahamian committed Jan 15, 2019
1 parent ec0e30e commit a3adb3f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/bulk/ordered.js
Expand Up @@ -12,6 +12,7 @@ const executeOperation = utils.executeOperation;
const MongoWriteConcernError = require('mongodb-core').MongoWriteConcernError;
const handleMongoWriteConcernError = require('./common').handleMongoWriteConcernError;
const bson = common.bson;
const isPromiseLike = require('../utils').isPromiseLike;

/**
* Add to internal list of Operations
Expand Down Expand Up @@ -114,6 +115,10 @@ class OrderedBulkOperation extends BulkOperationBase {
*/
execute(_writeConcern, options, callback) {
const ret = this.bulkExecute(_writeConcern, options, callback);
if (isPromiseLike(ret)) {
return ret;
}

options = ret.options;
callback = ret.callback;

Expand Down
5 changes: 5 additions & 0 deletions lib/bulk/unordered.js
Expand Up @@ -12,6 +12,7 @@ const executeOperation = utils.executeOperation;
const MongoWriteConcernError = require('mongodb-core').MongoWriteConcernError;
const handleMongoWriteConcernError = require('./common').handleMongoWriteConcernError;
const bson = common.bson;
const isPromiseLike = require('../utils').isPromiseLike;

/**
* Add to internal list of Operations
Expand Down Expand Up @@ -126,6 +127,10 @@ class UnorderedBulkOperation extends BulkOperationBase {
*/
execute(_writeConcern, options, callback) {
const ret = this.bulkExecute(_writeConcern, options, callback);
if (isPromiseLike(ret)) {
return ret;
}

options = ret.options;
callback = ret.callback;

Expand Down
36 changes: 36 additions & 0 deletions test/functional/bulk_tests.js
Expand Up @@ -3,6 +3,8 @@ const test = require('./shared').assert,
setupDatabase = require('./shared').setupDatabase,
expect = require('chai').expect;

const MongoError = require('../../index').MongoError;

describe('Bulk', function() {
before(function() {
return setupDatabase(this.configuration);
Expand Down Expand Up @@ -1571,4 +1573,38 @@ describe('Bulk', function() {
});
});
});

function testPropagationOfBulkWriteError(bulk) {
return bulk.execute().then(
err => {
expect(err).to.be.an.instanceOf(MongoError);
},
err => {
expect(err).to.be.an.instanceOf(MongoError);
expect(err).to.not.be.an.instanceOf(TypeError);
expect(err.driver).to.equal(true);
expect(err.name).to.equal('MongoError');
}
);
}

it('should propagate the proper error from executing an empty ordered batch', function() {
const client = this.configuration.newClient();

return client.connect().then(() => {
const collection = client.db(this.configuration.db).collection('doesnt_matter');

return testPropagationOfBulkWriteError(collection.initializeOrderedBulkOp());
});
});

it('should propagate the proper error from executing an empty unordered batch', function() {
const client = this.configuration.newClient();

return client.connect().then(() => {
const collection = client.db(this.configuration.db).collection('doesnt_matter');

return testPropagationOfBulkWriteError(collection.initializeUnorderedBulkOp());
});
});
});

0 comments on commit a3adb3f

Please sign in to comment.