@@ -5,6 +5,9 @@ const f = require('util').format;
5
5
const ReadPreference = require ( './read_preference' ) ;
6
6
const Buffer = require ( 'safe-buffer' ) . Buffer ;
7
7
const TopologyType = require ( '../sdam/topology_description' ) . TopologyType ;
8
+ const MongoError = require ( '../error' ) . MongoError ;
9
+
10
+ const MMAPv1_RETRY_WRITES_ERROR_CODE = 20 ;
8
11
9
12
/**
10
13
* Emit event if it exists
@@ -437,6 +440,24 @@ const isRetryableWritesSupported = function(topology) {
437
440
return true ;
438
441
} ;
439
442
443
+ const MMAPv1_RETRY_WRITES_ERROR_MESSAGE =
444
+ 'This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.' ;
445
+
446
+ function getMMAPError ( err ) {
447
+ if ( err . code !== MMAPv1_RETRY_WRITES_ERROR_CODE || ! err . errmsg . includes ( 'Transaction numbers' ) ) {
448
+ return err ;
449
+ }
450
+
451
+ // According to the retryable writes spec, we must replace the error message in this case.
452
+ // We need to replace err.message so the thrown message is correct and we need to replace err.errmsg to meet the spec requirement.
453
+ const newErr = new MongoError ( {
454
+ message : MMAPv1_RETRY_WRITES_ERROR_MESSAGE ,
455
+ errmsg : MMAPv1_RETRY_WRITES_ERROR_MESSAGE ,
456
+ originalError : err
457
+ } ) ;
458
+ return newErr ;
459
+ }
460
+
440
461
module . exports . SessionMixins = SessionMixins ;
441
462
module . exports . resolveClusterTime = resolveClusterTime ;
442
463
module . exports . inquireServerState = inquireServerState ;
@@ -451,3 +472,4 @@ module.exports.diff = diff;
451
472
module . exports . Interval = Interval ;
452
473
module . exports . Timeout = Timeout ;
453
474
module . exports . isRetryableWritesSupported = isRetryableWritesSupported ;
475
+ module . exports . getMMAPError = getMMAPError ;
0 commit comments