1
1
import { PromiseProvider } from '../promise_provider' ;
2
2
import { Long , ObjectId , Document , BSONSerializeOptions , resolveBSONOptions } from '../bson' ;
3
- import { MongoError , MongoWriteConcernError , AnyError , MONGODB_ERROR_CODES } from '../error' ;
3
+ import {
4
+ MongoWriteConcernError ,
5
+ AnyError ,
6
+ MONGODB_ERROR_CODES ,
7
+ MongoServerError ,
8
+ MongoDriverError
9
+ } from '../error' ;
4
10
import {
5
11
applyRetryableWrites ,
6
12
executeLegacyOperation ,
@@ -303,7 +309,7 @@ export class BulkWriteResult {
303
309
}
304
310
305
311
return new WriteConcernError (
306
- new MongoError ( { errmsg : errmsg , code : MONGODB_ERROR_CODES . WriteConcernFailed } )
312
+ new MongoServerError ( { errmsg : errmsg , code : MONGODB_ERROR_CODES . WriteConcernFailed } )
307
313
) ;
308
314
}
309
315
}
@@ -327,9 +333,9 @@ export class BulkWriteResult {
327
333
* @category Error
328
334
*/
329
335
export class WriteConcernError {
330
- err : MongoError ;
336
+ err : MongoServerError ;
331
337
332
- constructor ( err : MongoError ) {
338
+ constructor ( err : MongoServerError ) {
333
339
this . err = err ;
334
340
}
335
341
@@ -649,16 +655,17 @@ function handleMongoWriteConcernError(
649
655
) {
650
656
mergeBatchResults ( batch , bulkResult , undefined , err . result ) ;
651
657
658
+ // TODO: Remove multiple levels of wrapping (NODE-3337)
652
659
const wrappedWriteConcernError = new WriteConcernError (
653
- new MongoError ( {
660
+ new MongoServerError ( {
654
661
errmsg : err . result ?. writeConcernError . errmsg ,
655
662
code : err . result ?. writeConcernError . result
656
663
} )
657
664
) ;
658
665
659
666
callback (
660
667
new MongoBulkWriteError (
661
- new MongoError ( wrappedWriteConcernError ) ,
668
+ new MongoServerError ( wrappedWriteConcernError ) ,
662
669
new BulkWriteResult ( bulkResult )
663
670
)
664
671
) ;
@@ -669,7 +676,7 @@ function handleMongoWriteConcernError(
669
676
* @public
670
677
* @category Error
671
678
*/
672
- export class MongoBulkWriteError extends MongoError {
679
+ export class MongoBulkWriteError extends MongoServerError {
673
680
result : BulkWriteResult ;
674
681
675
682
/** Creates a new MongoBulkWriteError */
@@ -743,7 +750,7 @@ export class FindOperators {
743
750
/** Add a single update operation to the bulk operation */
744
751
updateOne ( updateDocument : Document ) : BulkOperationBase {
745
752
if ( ! hasAtomicOperators ( updateDocument ) ) {
746
- throw new TypeError ( 'Update document requires atomic operators' ) ;
753
+ throw new MongoDriverError ( 'Update document requires atomic operators' ) ;
747
754
}
748
755
749
756
const currentOp = buildCurrentOp ( this . bulkOperation ) ;
@@ -756,7 +763,7 @@ export class FindOperators {
756
763
/** Add a replace one operation to the bulk operation */
757
764
replaceOne ( replacement : Document ) : BulkOperationBase {
758
765
if ( hasAtomicOperators ( replacement ) ) {
759
- throw new TypeError ( 'Replacement document must not use atomic operators' ) ;
766
+ throw new MongoDriverError ( 'Replacement document must not use atomic operators' ) ;
760
767
}
761
768
762
769
const currentOp = buildCurrentOp ( this . bulkOperation ) ;
@@ -1039,7 +1046,7 @@ export abstract class BulkOperationBase {
1039
1046
*/
1040
1047
find ( selector : Document ) : FindOperators {
1041
1048
if ( ! selector ) {
1042
- throw TypeError ( 'Bulk find operation must specify a selector' ) ;
1049
+ throw new MongoDriverError ( 'Bulk find operation must specify a selector' ) ;
1043
1050
}
1044
1051
1045
1052
// Save a current selector
@@ -1073,51 +1080,51 @@ export abstract class BulkOperationBase {
1073
1080
if ( 'replaceOne' in op || 'updateOne' in op || 'updateMany' in op ) {
1074
1081
if ( 'replaceOne' in op ) {
1075
1082
if ( 'q' in op . replaceOne ) {
1076
- throw new TypeError ( 'Raw operations are not allowed' ) ;
1083
+ throw new MongoDriverError ( 'Raw operations are not allowed' ) ;
1077
1084
}
1078
1085
const updateStatement = makeUpdateStatement (
1079
1086
op . replaceOne . filter ,
1080
1087
op . replaceOne . replacement ,
1081
1088
{ ...op . replaceOne , multi : false }
1082
1089
) ;
1083
1090
if ( hasAtomicOperators ( updateStatement . u ) ) {
1084
- throw new TypeError ( 'Replacement document must not use atomic operators' ) ;
1091
+ throw new MongoDriverError ( 'Replacement document must not use atomic operators' ) ;
1085
1092
}
1086
1093
return this . addToOperationsList ( BatchType . UPDATE , updateStatement ) ;
1087
1094
}
1088
1095
1089
1096
if ( 'updateOne' in op ) {
1090
1097
if ( 'q' in op . updateOne ) {
1091
- throw new TypeError ( 'Raw operations are not allowed' ) ;
1098
+ throw new MongoDriverError ( 'Raw operations are not allowed' ) ;
1092
1099
}
1093
1100
const updateStatement = makeUpdateStatement ( op . updateOne . filter , op . updateOne . update , {
1094
1101
...op . updateOne ,
1095
1102
multi : false
1096
1103
} ) ;
1097
1104
if ( ! hasAtomicOperators ( updateStatement . u ) ) {
1098
- throw new TypeError ( 'Update document requires atomic operators' ) ;
1105
+ throw new MongoDriverError ( 'Update document requires atomic operators' ) ;
1099
1106
}
1100
1107
return this . addToOperationsList ( BatchType . UPDATE , updateStatement ) ;
1101
1108
}
1102
1109
1103
1110
if ( 'updateMany' in op ) {
1104
1111
if ( 'q' in op . updateMany ) {
1105
- throw new TypeError ( 'Raw operations are not allowed' ) ;
1112
+ throw new MongoDriverError ( 'Raw operations are not allowed' ) ;
1106
1113
}
1107
1114
const updateStatement = makeUpdateStatement ( op . updateMany . filter , op . updateMany . update , {
1108
1115
...op . updateMany ,
1109
1116
multi : true
1110
1117
} ) ;
1111
1118
if ( ! hasAtomicOperators ( updateStatement . u ) ) {
1112
- throw new TypeError ( 'Update document requires atomic operators' ) ;
1119
+ throw new MongoDriverError ( 'Update document requires atomic operators' ) ;
1113
1120
}
1114
1121
return this . addToOperationsList ( BatchType . UPDATE , updateStatement ) ;
1115
1122
}
1116
1123
}
1117
1124
1118
1125
if ( 'deleteOne' in op ) {
1119
1126
if ( 'q' in op . deleteOne ) {
1120
- throw new TypeError ( 'Raw operations are not allowed' ) ;
1127
+ throw new MongoDriverError ( 'Raw operations are not allowed' ) ;
1121
1128
}
1122
1129
return this . addToOperationsList (
1123
1130
BatchType . DELETE ,
@@ -1127,7 +1134,7 @@ export abstract class BulkOperationBase {
1127
1134
1128
1135
if ( 'deleteMany' in op ) {
1129
1136
if ( 'q' in op . deleteMany ) {
1130
- throw new TypeError ( 'Raw operations are not allowed' ) ;
1137
+ throw new MongoDriverError ( 'Raw operations are not allowed' ) ;
1131
1138
}
1132
1139
return this . addToOperationsList (
1133
1140
BatchType . DELETE ,
@@ -1136,7 +1143,7 @@ export abstract class BulkOperationBase {
1136
1143
}
1137
1144
1138
1145
// otherwise an unknown operation was provided
1139
- throw TypeError (
1146
+ throw new MongoDriverError (
1140
1147
'bulkWrite only supports insertOne, updateOne, updateMany, deleteOne, deleteMany'
1141
1148
) ;
1142
1149
}
@@ -1170,7 +1177,7 @@ export abstract class BulkOperationBase {
1170
1177
options = options ?? { } ;
1171
1178
1172
1179
if ( this . s . executed ) {
1173
- return handleEarlyError ( new MongoError ( 'Batch cannot be re-executed' ) , callback ) ;
1180
+ return handleEarlyError ( new MongoDriverError ( 'Batch cannot be re-executed' ) , callback ) ;
1174
1181
}
1175
1182
1176
1183
const writeConcern = WriteConcern . fromOptions ( options ) ;
@@ -1188,7 +1195,7 @@ export abstract class BulkOperationBase {
1188
1195
}
1189
1196
// If we have no operations in the bulk raise an error
1190
1197
if ( this . s . batches . length === 0 ) {
1191
- const emptyBatchError = new TypeError ( 'Invalid BulkOperation, Batch cannot be empty' ) ;
1198
+ const emptyBatchError = new MongoDriverError ( 'Invalid BulkOperation, Batch cannot be empty' ) ;
1192
1199
return handleEarlyError ( emptyBatchError , callback ) ;
1193
1200
}
1194
1201
@@ -1211,7 +1218,7 @@ export abstract class BulkOperationBase {
1211
1218
1212
1219
callback (
1213
1220
new MongoBulkWriteError (
1214
- new MongoError ( {
1221
+ new MongoServerError ( {
1215
1222
message : msg ,
1216
1223
code : this . s . bulkResult . writeErrors [ 0 ] . code ,
1217
1224
writeErrors : this . s . bulkResult . writeErrors
@@ -1225,7 +1232,7 @@ export abstract class BulkOperationBase {
1225
1232
1226
1233
const writeConcernError = writeResult . getWriteConcernError ( ) ;
1227
1234
if ( writeConcernError ) {
1228
- callback ( new MongoBulkWriteError ( new MongoError ( writeConcernError ) , writeResult ) ) ;
1235
+ callback ( new MongoBulkWriteError ( new MongoServerError ( writeConcernError ) , writeResult ) ) ;
1229
1236
return true ;
1230
1237
}
1231
1238
}
0 commit comments