88} = require ( './shared' ) ;
99const test = require ( './shared' ) . assert ;
1010const { MongoDriverError, MongoBatchReExecutionError } = require ( '../../src/error' ) ;
11- const { Long } = require ( '../../src' ) ;
11+ const { Long, MongoBulkWriteError } = require ( '../../src' ) ;
1212const crypto = require ( 'crypto' ) ;
1313const chai = require ( 'chai' ) ;
1414const 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 : / n o t a s t r i n g / } ] ) ;
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