Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-3434): errInfo should be exposed on bulk write #2977

Merged
merged 1 commit into from Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/bulk/common.ts
Expand Up @@ -383,6 +383,7 @@ export interface BulkWriteOperationError {
index: number;
code: number;
errmsg: string;
errInfo: Document;
op: Document | UpdateStatement | DeleteStatement;
}

Expand Down Expand Up @@ -413,6 +414,11 @@ export class WriteError {
return this.err.errmsg;
}

/** WriteError details. */
get errInfo(): Document | undefined {
return this.err.errInfo;
}

/** Returns the underlying operation that caused the error */
getOperation(): Document {
return this.err.op;
Expand Down Expand Up @@ -453,6 +459,7 @@ function mergeBatchResults(
index: 0,
code: result.code || 0,
errmsg: result.message,
errInfo: result.errInfo,
op: batch.operations[0]
};

Expand Down Expand Up @@ -555,6 +562,7 @@ function mergeBatchResults(
index: batch.originalIndexes[result.writeErrors[i].index],
code: result.writeErrors[i].code,
errmsg: result.writeErrors[i].errmsg,
errInfo: result.writeErrors[i].errInfo,
op: batch.operations[result.writeErrors[i].index]
};

Expand Down
44 changes: 43 additions & 1 deletion test/functional/bulk.test.js
Expand Up @@ -8,7 +8,7 @@ const {
} = require('./shared');
const test = require('./shared').assert;
const { MongoDriverError, MongoBatchReExecutionError } = require('../../src/error');
const { Long } = require('../../src');
const { Long, MongoBulkWriteError } = require('../../src');
const crypto = require('crypto');
const chai = require('chai');
const expect = chai.expect;
Expand All @@ -21,6 +21,48 @@ describe('Bulk', function () {
return setupDatabase(this.configuration);
});

describe('Write Errors', () => {
describe('errInfo property on insertMany', () => {
let client;

beforeEach(async function () {
client = this.configuration.newClient({ monitorCommands: true });
await client.connect();
});

afterEach(async () => {
if (client) {
await client.close();
}
});

it('should be accessible', {
metadata: { requires: { mongodb: '>=5.0.0' } },
async test() {
try {
await client.db().collection('wc_details').drop();
} catch {
// don't care
}

const collection = await client
.db()
.createCollection('wc_details', { validator: { x: { $type: 'string' } } });

try {
await collection.insertMany([{ x: /not a string/ }]);
expect.fail('The insert should fail the validation that x must be a string');
} catch (error) {
expect(error).to.be.instanceOf(MongoBulkWriteError);
expect(error).to.have.property('code', 121);
expect(error).to.have.property('writeErrors').that.is.an('array');
expect(error.writeErrors[0]).to.have.property('errInfo').that.is.an('object');
}
}
});
});
});

it('should correctly handle ordered single batch api write command error', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
Expand Down