From a3adb3f9e4be3e524d7a32cbe058b687a501e9d3 Mon Sep 17 00:00:00 2001 From: Dan Aprahamian Date: Tue, 15 Jan 2019 13:27:15 -0500 Subject: [PATCH] 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 --- lib/bulk/ordered.js | 5 +++++ lib/bulk/unordered.js | 5 +++++ test/functional/bulk_tests.js | 36 +++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/lib/bulk/ordered.js b/lib/bulk/ordered.js index 3ac05c5b46..9f8e4b5c7f 100644 --- a/lib/bulk/ordered.js +++ b/lib/bulk/ordered.js @@ -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 @@ -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; diff --git a/lib/bulk/unordered.js b/lib/bulk/unordered.js index 6ec27b9081..563777bfdf 100644 --- a/lib/bulk/unordered.js +++ b/lib/bulk/unordered.js @@ -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 @@ -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; diff --git a/test/functional/bulk_tests.js b/test/functional/bulk_tests.js index d7c98299f0..68125617b9 100644 --- a/test/functional/bulk_tests.js +++ b/test/functional/bulk_tests.js @@ -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); @@ -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()); + }); + }); });