8
8
} = require ( './shared' ) ;
9
9
const test = require ( './shared' ) . assert ;
10
10
const { MongoDriverError, MongoBatchReExecutionError } = require ( '../../src/error' ) ;
11
- const { Long } = require ( '../../src' ) ;
11
+ const { Long, MongoBulkWriteError } = require ( '../../src' ) ;
12
12
const crypto = require ( 'crypto' ) ;
13
13
const chai = require ( 'chai' ) ;
14
14
const expect = chai . expect ;
@@ -21,6 +21,48 @@ describe('Bulk', function () {
21
21
return setupDatabase ( this . configuration ) ;
22
22
} ) ;
23
23
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
+
24
66
it ( 'should correctly handle ordered single batch api write command error' , {
25
67
metadata : {
26
68
requires : { topology : [ 'single' , 'replicaset' , 'sharded' , 'ssl' , 'heap' , 'wiredtiger' ] }
0 commit comments