1
1
import type { Document } from './bson' ;
2
+ import { ServerType } from './sdam/common' ;
2
3
import type { TopologyVersion } from './sdam/server_description' ;
3
4
import type { TopologyDescription } from './sdam/topology_description' ;
4
5
@@ -1217,10 +1218,18 @@ const RETRYABLE_READ_ERROR_CODES = new Set<number>([
1217
1218
// see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#terms
1218
1219
const RETRYABLE_WRITE_ERROR_CODES = RETRYABLE_READ_ERROR_CODES ;
1219
1220
1221
+ const REPLICASET_SERVER_TYPES : ServerType [ ] = [
1222
+ ServerType . RSPrimary ,
1223
+ ServerType . RSSecondary ,
1224
+ ServerType . RSArbiter ,
1225
+ ServerType . RSOther ,
1226
+ ServerType . RSGhost
1227
+ ] ;
1228
+
1220
1229
export function needsRetryableWriteLabel (
1221
1230
error : Error ,
1222
1231
maxWireVersion : number ,
1223
- isSharded : boolean
1232
+ serverType : ServerType
1224
1233
) : boolean {
1225
1234
// pre-4.4 server, then the driver adds an error label for every valid case
1226
1235
// execute operation will only inspect the label, code/message logic is handled here
@@ -1240,14 +1249,25 @@ export function needsRetryableWriteLabel(
1240
1249
}
1241
1250
}
1242
1251
1243
- // For writeConcernErrors from a pre-4.4 mongos response, do not use the error code to determine retryability.
1244
- const isShardedAndPre4_4 = isSharded && maxWireVersion < 9 ;
1245
- if ( error instanceof MongoWriteConcernError && ! isShardedAndPre4_4 ) {
1246
- return RETRYABLE_WRITE_ERROR_CODES . has ( error . result ?. code ?? error . code ?? 0 ) ;
1252
+ // Logic for pre-4.4 server responses from mongos and mongod servers
1253
+ if ( maxWireVersion < 9 && [ 'Mongos' , ...REPLICASET_SERVER_TYPES ] . includes ( serverType ) ) {
1254
+ // In pre-4.4 servers, use error code to determine retryability for writeConcernErrors in only mongod responses.
1255
+ if ( REPLICASET_SERVER_TYPES . includes ( serverType ) && error instanceof MongoWriteConcernError ) {
1256
+ return RETRYABLE_WRITE_ERROR_CODES . has ( error . result ?. code ?? Number ( error . code ) ?? 0 ) ;
1257
+ }
1258
+ // In pre-4.4 servers, use error code to determine retryability for non-writeConcernErrors in mongod and mongos responses.
1259
+ if ( ! ( error instanceof MongoWriteConcernError ) && error instanceof MongoError ) {
1260
+ return RETRYABLE_WRITE_ERROR_CODES . has ( Number ( error . code ) ?? 0 ) ;
1261
+ }
1262
+ return false ;
1263
+ }
1264
+
1265
+ if ( error instanceof MongoWriteConcernError ) {
1266
+ return RETRYABLE_WRITE_ERROR_CODES . has ( error . result ?. code ?? Number ( error . code ) ?? 0 ) ;
1247
1267
}
1248
1268
1249
1269
if ( error instanceof MongoError && typeof error . code === 'number' ) {
1250
- return RETRYABLE_WRITE_ERROR_CODES . has ( error . code ) ;
1270
+ return RETRYABLE_WRITE_ERROR_CODES . has ( Number ( error . code ) ) ;
1251
1271
}
1252
1272
1253
1273
const isNotWritablePrimaryError = LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE . test ( error . message ) ;
0 commit comments