Skip to content

Commit 6b3c161

Browse files
authored
fix(NODE-3434): errInfo should be exposed on bulk write (#2977)
1 parent f1b896d commit 6b3c161

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/bulk/common.ts

+8
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ export interface BulkWriteOperationError {
383383
index: number;
384384
code: number;
385385
errmsg: string;
386+
errInfo: Document;
386387
op: Document | UpdateStatement | DeleteStatement;
387388
}
388389

@@ -413,6 +414,11 @@ export class WriteError {
413414
return this.err.errmsg;
414415
}
415416

417+
/** WriteError details. */
418+
get errInfo(): Document | undefined {
419+
return this.err.errInfo;
420+
}
421+
416422
/** Returns the underlying operation that caused the error */
417423
getOperation(): Document {
418424
return this.err.op;
@@ -453,6 +459,7 @@ function mergeBatchResults(
453459
index: 0,
454460
code: result.code || 0,
455461
errmsg: result.message,
462+
errInfo: result.errInfo,
456463
op: batch.operations[0]
457464
};
458465

@@ -555,6 +562,7 @@ function mergeBatchResults(
555562
index: batch.originalIndexes[result.writeErrors[i].index],
556563
code: result.writeErrors[i].code,
557564
errmsg: result.writeErrors[i].errmsg,
565+
errInfo: result.writeErrors[i].errInfo,
558566
op: batch.operations[result.writeErrors[i].index]
559567
};
560568

test/functional/bulk.test.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {
88
} = require('./shared');
99
const test = require('./shared').assert;
1010
const { MongoDriverError, MongoBatchReExecutionError } = require('../../src/error');
11-
const { Long } = require('../../src');
11+
const { Long, MongoBulkWriteError } = require('../../src');
1212
const crypto = require('crypto');
1313
const chai = require('chai');
1414
const expect = chai.expect;
@@ -21,6 +21,48 @@ describe('Bulk', function () {
2121
return setupDatabase(this.configuration);
2222
});
2323

24+
describe('Write Errors', () => {
25+
describe('errInfo property on insertMany', () => {
26+
let client;
27+
28+
beforeEach(async function () {
29+
client = this.configuration.newClient({ monitorCommands: true });
30+
await client.connect();
31+
});
32+
33+
afterEach(async () => {
34+
if (client) {
35+
await client.close();
36+
}
37+
});
38+
39+
it('should be accessible', {
40+
metadata: { requires: { mongodb: '>=5.0.0' } },
41+
async test() {
42+
try {
43+
await client.db().collection('wc_details').drop();
44+
} catch {
45+
// don't care
46+
}
47+
48+
const collection = await client
49+
.db()
50+
.createCollection('wc_details', { validator: { x: { $type: 'string' } } });
51+
52+
try {
53+
await collection.insertMany([{ x: /not a string/ }]);
54+
expect.fail('The insert should fail the validation that x must be a string');
55+
} catch (error) {
56+
expect(error).to.be.instanceOf(MongoBulkWriteError);
57+
expect(error).to.have.property('code', 121);
58+
expect(error).to.have.property('writeErrors').that.is.an('array');
59+
expect(error.writeErrors[0]).to.have.property('errInfo').that.is.an('object');
60+
}
61+
}
62+
});
63+
});
64+
});
65+
2466
it('should correctly handle ordered single batch api write command error', {
2567
metadata: {
2668
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }

0 commit comments

Comments
 (0)