11import type { Document } from './bson' ;
2+ import type { ServerType } from './sdam/common' ;
23import type { TopologyVersion } from './sdam/server_description' ;
34import type { TopologyDescription } from './sdam/topology_description' ;
45
@@ -1226,7 +1227,11 @@ const RETRYABLE_READ_ERROR_CODES = new Set<number>([
12261227// see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#terms
12271228const RETRYABLE_WRITE_ERROR_CODES = RETRYABLE_READ_ERROR_CODES ;
12281229
1229- export function needsRetryableWriteLabel ( error : Error , maxWireVersion : number ) : boolean {
1230+ export function needsRetryableWriteLabel (
1231+ error : Error ,
1232+ maxWireVersion : number ,
1233+ serverType : ServerType
1234+ ) : boolean {
12301235 // pre-4.4 server, then the driver adds an error label for every valid case
12311236 // execute operation will only inspect the label, code/message logic is handled here
12321237 if ( error instanceof MongoNetworkError ) {
@@ -1246,11 +1251,17 @@ export function needsRetryableWriteLabel(error: Error, maxWireVersion: number):
12461251 }
12471252
12481253 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+ ) ;
12501261 }
12511262
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 ) ) ;
12541265 }
12551266
12561267 const isNotWritablePrimaryError = LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE . test ( error . message ) ;
0 commit comments