Skip to content

Commit 6bcf1e4

Browse files
Sam Paldaprahamian
Sam Pal
authored andcommitted
fix(Bulk): change BulkWriteError message to first item from writeErrors (#2013)
* fix(Bulk): change BulkWriteError message to first item from writeErrors Fixes NODE-1965
1 parent 348d82a commit 6bcf1e4

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

lib/bulk/common.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1115,11 +1115,15 @@ class BulkOperationBase {
11151115
return true;
11161116
}
11171117

1118+
const msg = this.s.bulkResult.writeErrors[0].errmsg
1119+
? this.s.bulkResult.writeErrors[0].errmsg
1120+
: 'write operation failed';
1121+
11181122
handleCallback(
11191123
callback,
11201124
new BulkWriteError(
11211125
toError({
1122-
message: 'write operation failed',
1126+
message: msg,
11231127
code: this.s.bulkResult.writeErrors[0].code,
11241128
writeErrors: this.s.bulkResult.writeErrors
11251129
}),

test/functional/bulk_tests.js

+53
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,59 @@ describe('Bulk', function() {
937937
}
938938
});
939939

940+
it('should provide descriptive error message for unordered batch with duplicate key errors on inserts', function(done) {
941+
const configuration = this.configuration;
942+
const client = configuration.newClient(configuration.writeConcernMax(), {
943+
poolSize: 1
944+
});
945+
946+
client.connect((err, client) => {
947+
const db = client.db(configuration.db);
948+
const col = db.collection('err_batch_write_unordered_ops_legacy_6');
949+
950+
// Add unique index on a field causing all inserts to fail
951+
col.createIndexes(
952+
[
953+
{
954+
name: 'err_batch_write_unordered_ops_legacy_6',
955+
key: { a: 1 },
956+
unique: true
957+
}
958+
],
959+
err => {
960+
expect(err).to.not.exist;
961+
962+
// Initialize the unordered Batch
963+
const batch = col.initializeUnorderedBulkOp();
964+
965+
// Add some operations to be executed in order
966+
batch.insert({ a: 1 });
967+
batch.insert({ a: 1 });
968+
969+
// Execute the operations
970+
batch.execute(configuration.writeConcernMax(), (err, result) => {
971+
expect(err).to.exist;
972+
expect(result).to.not.exist;
973+
974+
// Test basic settings
975+
result = err.result;
976+
expect(result.nInserted).to.equal(1);
977+
expect(result.hasWriteErrors()).to.equal(true);
978+
expect(result.getWriteErrorCount() === 1).to.equal(true);
979+
980+
// Individual error checking
981+
const error = result.getWriteErrorAt(0);
982+
expect(error.code === 11000).to.equal(true);
983+
expect(error.errmsg).to.exist;
984+
expect(err.message).to.equal(error.errmsg);
985+
986+
client.close(done);
987+
});
988+
}
989+
);
990+
});
991+
});
992+
940993
it(
941994
'should Correctly Execute Unordered Batch of with upserts causing duplicate key errors on updates',
942995
{

0 commit comments

Comments
 (0)