@@ -5,6 +5,8 @@ const { ReplSetFixture } = require('./common');
5
5
const { MongoWriteConcernError } = require ( '../../../src/error' ) ;
6
6
const { expect } = require ( 'chai' ) ;
7
7
const { ns } = require ( '../../../src/utils' ) ;
8
+ const { once } = require ( 'events' ) ;
9
+ const { MongoServerError } = require ( '../../../src' ) ;
8
10
9
11
describe ( 'WriteConcernError' , function ( ) {
10
12
let test ;
@@ -159,4 +161,56 @@ describe('WriteConcernError', function () {
159
161
} ) ;
160
162
} ) ;
161
163
} ) ;
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 : / n o t a s t r i n g / } ) ;
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
+ } ) ;
162
216
} ) ;
0 commit comments