5
5
AnyError ,
6
6
MONGODB_ERROR_CODES ,
7
7
MongoServerError ,
8
- MongoDriverError
8
+ MongoDriverError ,
9
+ MongoInvalidArgumentError
9
10
} from '../error' ;
10
11
import {
11
12
applyRetryableWrites ,
@@ -753,7 +754,7 @@ export class FindOperators {
753
754
/** Add a single update operation to the bulk operation */
754
755
updateOne ( updateDocument : Document ) : BulkOperationBase {
755
756
if ( ! hasAtomicOperators ( updateDocument ) ) {
756
- throw new MongoDriverError ( 'Update document requires atomic operators' ) ;
757
+ throw new MongoInvalidArgumentError ( 'Update document requires atomic operators' ) ;
757
758
}
758
759
759
760
const currentOp = buildCurrentOp ( this . bulkOperation ) ;
@@ -766,7 +767,7 @@ export class FindOperators {
766
767
/** Add a replace one operation to the bulk operation */
767
768
replaceOne ( replacement : Document ) : BulkOperationBase {
768
769
if ( hasAtomicOperators ( replacement ) ) {
769
- throw new MongoDriverError ( 'Replacement document must not use atomic operators' ) ;
770
+ throw new MongoInvalidArgumentError ( 'Replacement document must not use atomic operators' ) ;
770
771
}
771
772
772
773
const currentOp = buildCurrentOp ( this . bulkOperation ) ;
@@ -1049,7 +1050,7 @@ export abstract class BulkOperationBase {
1049
1050
*/
1050
1051
find ( selector : Document ) : FindOperators {
1051
1052
if ( ! selector ) {
1052
- throw new MongoDriverError ( 'Bulk find operation must specify a selector' ) ;
1053
+ throw new MongoInvalidArgumentError ( 'Bulk find operation must specify a selector' ) ;
1053
1054
}
1054
1055
1055
1056
// Save a current selector
@@ -1083,51 +1084,51 @@ export abstract class BulkOperationBase {
1083
1084
if ( 'replaceOne' in op || 'updateOne' in op || 'updateMany' in op ) {
1084
1085
if ( 'replaceOne' in op ) {
1085
1086
if ( 'q' in op . replaceOne ) {
1086
- throw new MongoDriverError ( 'Raw operations are not allowed' ) ;
1087
+ throw new MongoInvalidArgumentError ( 'Raw operations are not allowed' ) ;
1087
1088
}
1088
1089
const updateStatement = makeUpdateStatement (
1089
1090
op . replaceOne . filter ,
1090
1091
op . replaceOne . replacement ,
1091
1092
{ ...op . replaceOne , multi : false }
1092
1093
) ;
1093
1094
if ( hasAtomicOperators ( updateStatement . u ) ) {
1094
- throw new MongoDriverError ( 'Replacement document must not use atomic operators' ) ;
1095
+ throw new MongoInvalidArgumentError ( 'Replacement document must not use atomic operators' ) ;
1095
1096
}
1096
1097
return this . addToOperationsList ( BatchType . UPDATE , updateStatement ) ;
1097
1098
}
1098
1099
1099
1100
if ( 'updateOne' in op ) {
1100
1101
if ( 'q' in op . updateOne ) {
1101
- throw new MongoDriverError ( 'Raw operations are not allowed' ) ;
1102
+ throw new MongoInvalidArgumentError ( 'Raw operations are not allowed' ) ;
1102
1103
}
1103
1104
const updateStatement = makeUpdateStatement ( op . updateOne . filter , op . updateOne . update , {
1104
1105
...op . updateOne ,
1105
1106
multi : false
1106
1107
} ) ;
1107
1108
if ( ! hasAtomicOperators ( updateStatement . u ) ) {
1108
- throw new MongoDriverError ( 'Update document requires atomic operators' ) ;
1109
+ throw new MongoInvalidArgumentError ( 'Update document requires atomic operators' ) ;
1109
1110
}
1110
1111
return this . addToOperationsList ( BatchType . UPDATE , updateStatement ) ;
1111
1112
}
1112
1113
1113
1114
if ( 'updateMany' in op ) {
1114
1115
if ( 'q' in op . updateMany ) {
1115
- throw new MongoDriverError ( 'Raw operations are not allowed' ) ;
1116
+ throw new MongoInvalidArgumentError ( 'Raw operations are not allowed' ) ;
1116
1117
}
1117
1118
const updateStatement = makeUpdateStatement ( op . updateMany . filter , op . updateMany . update , {
1118
1119
...op . updateMany ,
1119
1120
multi : true
1120
1121
} ) ;
1121
1122
if ( ! hasAtomicOperators ( updateStatement . u ) ) {
1122
- throw new MongoDriverError ( 'Update document requires atomic operators' ) ;
1123
+ throw new MongoInvalidArgumentError ( 'Update document requires atomic operators' ) ;
1123
1124
}
1124
1125
return this . addToOperationsList ( BatchType . UPDATE , updateStatement ) ;
1125
1126
}
1126
1127
}
1127
1128
1128
1129
if ( 'deleteOne' in op ) {
1129
1130
if ( 'q' in op . deleteOne ) {
1130
- throw new MongoDriverError ( 'Raw operations are not allowed' ) ;
1131
+ throw new MongoInvalidArgumentError ( 'Raw operations are not allowed' ) ;
1131
1132
}
1132
1133
return this . addToOperationsList (
1133
1134
BatchType . DELETE ,
@@ -1137,7 +1138,7 @@ export abstract class BulkOperationBase {
1137
1138
1138
1139
if ( 'deleteMany' in op ) {
1139
1140
if ( 'q' in op . deleteMany ) {
1140
- throw new MongoDriverError ( 'Raw operations are not allowed' ) ;
1141
+ throw new MongoInvalidArgumentError ( 'Raw operations are not allowed' ) ;
1141
1142
}
1142
1143
return this . addToOperationsList (
1143
1144
BatchType . DELETE ,
@@ -1146,7 +1147,7 @@ export abstract class BulkOperationBase {
1146
1147
}
1147
1148
1148
1149
// otherwise an unknown operation was provided
1149
- throw new MongoDriverError (
1150
+ throw new MongoInvalidArgumentError (
1150
1151
'bulkWrite only supports insertOne, updateOne, updateMany, deleteOne, deleteMany'
1151
1152
) ;
1152
1153
}
@@ -1198,7 +1199,9 @@ export abstract class BulkOperationBase {
1198
1199
}
1199
1200
// If we have no operations in the bulk raise an error
1200
1201
if ( this . s . batches . length === 0 ) {
1201
- const emptyBatchError = new MongoDriverError ( 'Invalid BulkOperation, Batch cannot be empty' ) ;
1202
+ const emptyBatchError = new MongoInvalidArgumentError (
1203
+ 'Invalid BulkOperation, Batch cannot be empty'
1204
+ ) ;
1202
1205
return handleEarlyError ( emptyBatchError , callback ) ;
1203
1206
}
1204
1207
0 commit comments