1
1
import type { Document } from './bson' ;
2
+ import type { ServerType } from './sdam/common' ;
2
3
import type { TopologyVersion } from './sdam/server_description' ;
3
4
import type { TopologyDescription } from './sdam/topology_description' ;
4
5
@@ -1226,7 +1227,11 @@ const RETRYABLE_READ_ERROR_CODES = new Set<number>([
1226
1227
// see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#terms
1227
1228
const RETRYABLE_WRITE_ERROR_CODES = RETRYABLE_READ_ERROR_CODES ;
1228
1229
1229
- export function needsRetryableWriteLabel ( error : Error , maxWireVersion : number ) : boolean {
1230
+ export function needsRetryableWriteLabel (
1231
+ error : Error ,
1232
+ maxWireVersion : number ,
1233
+ serverType : ServerType
1234
+ ) : boolean {
1230
1235
// pre-4.4 server, then the driver adds an error label for every valid case
1231
1236
// execute operation will only inspect the label, code/message logic is handled here
1232
1237
if ( error instanceof MongoNetworkError ) {
@@ -1246,11 +1251,17 @@ export function needsRetryableWriteLabel(error: Error, maxWireVersion: number):
1246
1251
}
1247
1252
1248
1253
if ( error instanceof MongoWriteConcernError ) {
1249
- return RETRYABLE_WRITE_ERROR_CODES . has ( error . result . writeConcernError . code ?? error ?. code ?? 0 ) ;
1254
+ if ( serverType === 'Mongos' && maxWireVersion < 9 ) {
1255
+ // use original top-level code from server response
1256
+ return RETRYABLE_WRITE_ERROR_CODES . has ( error . result . code ?? 0 ) ;
1257
+ }
1258
+ return RETRYABLE_WRITE_ERROR_CODES . has (
1259
+ error . result . writeConcernError . code ?? Number ( error . code ) ?? 0
1260
+ ) ;
1250
1261
}
1251
1262
1252
- if ( error instanceof MongoError && typeof error . code === 'number' ) {
1253
- return RETRYABLE_WRITE_ERROR_CODES . has ( error . code ) ;
1263
+ if ( error instanceof MongoError ) {
1264
+ return RETRYABLE_WRITE_ERROR_CODES . has ( Number ( error . code ) ) ;
1254
1265
}
1255
1266
1256
1267
const isNotWritablePrimaryError = LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE . test ( error . message ) ;
0 commit comments