Skip to content

Commit

Permalink
fix(Bulk): change BulkWriteError message to first item from writeErro…
Browse files Browse the repository at this point in the history
…rs (#2013)

* fix(Bulk): change BulkWriteError message to first item from writeErrors

Fixes NODE-1965
  • Loading branch information
Sam Pal authored and daprahamian committed Aug 13, 2019
1 parent 348d82a commit 6bcf1e4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/bulk/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -1115,11 +1115,15 @@ class BulkOperationBase {
return true;
}

const msg = this.s.bulkResult.writeErrors[0].errmsg
? this.s.bulkResult.writeErrors[0].errmsg
: 'write operation failed';

handleCallback(
callback,
new BulkWriteError(
toError({
message: 'write operation failed',
message: msg,
code: this.s.bulkResult.writeErrors[0].code,
writeErrors: this.s.bulkResult.writeErrors
}),
Expand Down
53 changes: 53 additions & 0 deletions test/functional/bulk_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,59 @@ describe('Bulk', function() {
}
});

it('should provide descriptive error message for unordered batch with duplicate key errors on inserts', function(done) {
const configuration = this.configuration;
const client = configuration.newClient(configuration.writeConcernMax(), {
poolSize: 1
});

client.connect((err, client) => {
const db = client.db(configuration.db);
const col = db.collection('err_batch_write_unordered_ops_legacy_6');

// Add unique index on a field causing all inserts to fail
col.createIndexes(
[
{
name: 'err_batch_write_unordered_ops_legacy_6',
key: { a: 1 },
unique: true
}
],
err => {
expect(err).to.not.exist;

// Initialize the unordered Batch
const batch = col.initializeUnorderedBulkOp();

// Add some operations to be executed in order
batch.insert({ a: 1 });
batch.insert({ a: 1 });

// Execute the operations
batch.execute(configuration.writeConcernMax(), (err, result) => {
expect(err).to.exist;
expect(result).to.not.exist;

// Test basic settings
result = err.result;
expect(result.nInserted).to.equal(1);
expect(result.hasWriteErrors()).to.equal(true);
expect(result.getWriteErrorCount() === 1).to.equal(true);

// Individual error checking
const error = result.getWriteErrorAt(0);
expect(error.code === 11000).to.equal(true);
expect(error.errmsg).to.exist;
expect(err.message).to.equal(error.errmsg);

client.close(done);
});
}
);
});
});

it(
'should Correctly Execute Unordered Batch of with upserts causing duplicate key errors on updates',
{
Expand Down

0 comments on commit 6bcf1e4

Please sign in to comment.