Skip to content

Commit e786b4d

Browse files
authored
test(NODE-3082): details property from write errors is accessible (#2907)
1 parent f87f376 commit e786b4d

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/unit/core/write_concern_error.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const { ReplSetFixture } = require('./common');
55
const { MongoWriteConcernError } = require('../../../src/error');
66
const { expect } = require('chai');
77
const { ns } = require('../../../src/utils');
8+
const { once } = require('events');
9+
const { MongoServerError } = require('../../../src');
810

911
describe('WriteConcernError', function () {
1012
let test;
@@ -159,4 +161,56 @@ describe('WriteConcernError', function () {
159161
});
160162
});
161163
});
164+
165+
describe('errInfo property', () => {
166+
let client;
167+
168+
beforeEach(async function () {
169+
client = this.configuration.newClient({ monitorCommands: true });
170+
await client.connect();
171+
});
172+
173+
afterEach(async () => {
174+
if (client) {
175+
await client.close();
176+
client.removeAllListeners();
177+
}
178+
});
179+
180+
it('should always be accessible', {
181+
metadata: { requires: { mongodb: '>=5.0.0' } },
182+
async test() {
183+
try {
184+
await client.db().collection('wc_details').drop();
185+
} catch {
186+
// don't care
187+
}
188+
189+
const collection = await client
190+
.db()
191+
.createCollection('wc_details', { validator: { x: { $type: 'string' } } });
192+
193+
const evCapture = once(client, 'commandSucceeded');
194+
195+
let errInfoFromError;
196+
try {
197+
await collection.insertOne({ x: /not a string/ });
198+
expect.fail('The insert should fail the validation that x must be a string');
199+
} catch (error) {
200+
expect(error).to.be.instanceOf(MongoServerError);
201+
expect(error).to.have.property('code', 121);
202+
expect(error).to.have.property('errInfo').that.is.an('object');
203+
errInfoFromError = error.errInfo;
204+
}
205+
206+
const commandSucceededEvents = await evCapture;
207+
expect(commandSucceededEvents).to.have.lengthOf(1);
208+
const ev = commandSucceededEvents[0];
209+
expect(ev).to.have.nested.property('reply.writeErrors[0].errInfo').that.is.an('object');
210+
211+
const errInfoFromEvent = ev.reply.writeErrors[0].errInfo;
212+
expect(errInfoFromError).to.deep.equal(errInfoFromEvent);
213+
}
214+
});
215+
});
162216
});

0 commit comments

Comments
 (0)