Skip to content

Commit

Permalink
fix: report writeErrors and writeConcernErrors for bulk ops
Browse files Browse the repository at this point in the history
Collates the `writeErrors` and `writeConcernErrors` reported by bulk operations
and returns them in the `result` object.
  • Loading branch information
Srinand Balaji committed Apr 16, 2020
1 parent 919d2c3 commit a121cc1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/bulk.js
Expand Up @@ -83,6 +83,17 @@ class Bulk {
const setResult = (cmd, cmdResult) => {
const cmdKey = Object.keys(cmd)[0];
result[this.cmdKeys[cmdKey]] += cmdResult.n;
// Collate and report writeErrors and writeConcernErrors to the result object.
if (cmdResult.writeErrors) {
for (const writeError of cmdResult.writeErrors) {
result.writeErrors.push(writeError);
}
}
if (cmdResult.writeConcernErrors) {
for (const writeConcernError of cmdResult.writeConcernErrors) {
result.writeErrors.push(writeConcernError);
}
}
}

return this
Expand Down
21 changes: 20 additions & 1 deletion test/bulk.js
Expand Up @@ -167,4 +167,23 @@ describe('bulk', function() {
expect(json.nRemoveOps).to.equal(1, 'Should result in nRemoveOps field set to 1');
expect(json.nBatches).to.equal(3, 'Should result in nBatches field set to 3');
});
});

it('should collate and report writeErrors', async () => {
await db.b.createIndex(
{
name: 1,
},
{
unique: true,
background: true,
}
);

const bulk = db.b.initializeUnorderedBulkOp();
bulk.insert({ name: 'Charmander' });
bulk.insert({ name: 'Charmander' });
const result = await bulk.execute();
expect(result.writeErrors.length).to.equal(1);
expect(result.writeErrors[0].errmsg).to.match(/duplicate key error/);
});
});

0 comments on commit a121cc1

Please sign in to comment.