@@ -8,6 +8,27 @@ export type AnyError = MongoError | Error;
8
8
/** @internal */
9
9
const kErrorLabels = Symbol ( 'errorLabels' ) ;
10
10
11
+ /**
12
+ * @internal
13
+ * The legacy error message from the server that indicates the node is not a writable primary
14
+ * https://github.com/mongodb/specifications/blob/b07c26dc40d04ac20349f989db531c9845fdd755/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#not-writable-primary-and-node-is-recovering
15
+ */
16
+ export const LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE = 'not master' ;
17
+
18
+ /**
19
+ * @internal
20
+ * The legacy error message from the server that indicates the node is not a primary or secondary
21
+ * https://github.com/mongodb/specifications/blob/b07c26dc40d04ac20349f989db531c9845fdd755/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#not-writable-primary-and-node-is-recovering
22
+ */
23
+ export const LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE = 'not master or secondary' ;
24
+
25
+ /**
26
+ * @internal
27
+ * The error message from the server that indicates the node is recovering
28
+ * https://github.com/mongodb/specifications/blob/b07c26dc40d04ac20349f989db531c9845fdd755/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#not-writable-primary-and-node-is-recovering
29
+ */
30
+ export const NODE_IS_RECOVERING_ERROR_MESSAGE = 'node is recovering' ;
31
+
11
32
/** @internal MongoDB Error Codes */
12
33
export const MONGODB_ERROR_CODES = Object . freeze ( {
13
34
HostUnreachable : 6 ,
@@ -17,11 +38,11 @@ export const MONGODB_ERROR_CODES = Object.freeze({
17
38
PrimarySteppedDown : 189 ,
18
39
ExceededTimeLimit : 262 ,
19
40
SocketException : 9001 ,
20
- NotMaster : 10107 ,
41
+ NotWritablePrimary : 10107 ,
21
42
InterruptedAtShutdown : 11600 ,
22
43
InterruptedDueToReplStateChange : 11602 ,
23
- NotMasterNoSlaveOk : 13435 ,
24
- NotMasterOrSecondary : 13436 ,
44
+ NotPrimaryNoSecondaryOk : 13435 ,
45
+ NotPrimaryOrSecondary : 13436 ,
25
46
StaleShardVersion : 63 ,
26
47
StaleEpoch : 150 ,
27
48
StaleConfig : 13388 ,
@@ -46,11 +67,11 @@ export const GET_MORE_RESUMABLE_CODES = new Set<number>([
46
67
MONGODB_ERROR_CODES . PrimarySteppedDown ,
47
68
MONGODB_ERROR_CODES . ExceededTimeLimit ,
48
69
MONGODB_ERROR_CODES . SocketException ,
49
- MONGODB_ERROR_CODES . NotMaster ,
70
+ MONGODB_ERROR_CODES . NotWritablePrimary ,
50
71
MONGODB_ERROR_CODES . InterruptedAtShutdown ,
51
72
MONGODB_ERROR_CODES . InterruptedDueToReplStateChange ,
52
- MONGODB_ERROR_CODES . NotMasterNoSlaveOk ,
53
- MONGODB_ERROR_CODES . NotMasterOrSecondary ,
73
+ MONGODB_ERROR_CODES . NotPrimaryNoSecondaryOk ,
74
+ MONGODB_ERROR_CODES . NotPrimaryOrSecondary ,
54
75
MONGODB_ERROR_CODES . StaleShardVersion ,
55
76
MONGODB_ERROR_CODES . StaleEpoch ,
56
77
MONGODB_ERROR_CODES . StaleConfig ,
@@ -675,19 +696,19 @@ const RETRYABLE_ERROR_CODES = new Set<number>([
675
696
MONGODB_ERROR_CODES . ShutdownInProgress ,
676
697
MONGODB_ERROR_CODES . PrimarySteppedDown ,
677
698
MONGODB_ERROR_CODES . SocketException ,
678
- MONGODB_ERROR_CODES . NotMaster ,
699
+ MONGODB_ERROR_CODES . NotWritablePrimary ,
679
700
MONGODB_ERROR_CODES . InterruptedAtShutdown ,
680
701
MONGODB_ERROR_CODES . InterruptedDueToReplStateChange ,
681
- MONGODB_ERROR_CODES . NotMasterNoSlaveOk ,
682
- MONGODB_ERROR_CODES . NotMasterOrSecondary
702
+ MONGODB_ERROR_CODES . NotPrimaryNoSecondaryOk ,
703
+ MONGODB_ERROR_CODES . NotPrimaryOrSecondary
683
704
] ) ;
684
705
685
706
const RETRYABLE_WRITE_ERROR_CODES = new Set < number > ( [
686
707
MONGODB_ERROR_CODES . InterruptedAtShutdown ,
687
708
MONGODB_ERROR_CODES . InterruptedDueToReplStateChange ,
688
- MONGODB_ERROR_CODES . NotMaster ,
689
- MONGODB_ERROR_CODES . NotMasterNoSlaveOk ,
690
- MONGODB_ERROR_CODES . NotMasterOrSecondary ,
709
+ MONGODB_ERROR_CODES . NotWritablePrimary ,
710
+ MONGODB_ERROR_CODES . NotPrimaryNoSecondaryOk ,
711
+ MONGODB_ERROR_CODES . NotPrimaryOrSecondary ,
691
712
MONGODB_ERROR_CODES . PrimarySteppedDown ,
692
713
MONGODB_ERROR_CODES . ShutdownInProgress ,
693
714
MONGODB_ERROR_CODES . HostNotFound ,
@@ -714,8 +735,8 @@ export function isRetryableError(error: MongoError): boolean {
714
735
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
715
736
( typeof error . code === 'number' && RETRYABLE_ERROR_CODES . has ( error . code ! ) ) ||
716
737
error instanceof MongoNetworkError ||
717
- ! ! error . message . match ( / n o t m a s t e r / ) ||
718
- ! ! error . message . match ( / n o d e i s r e c o v e r i n g / )
738
+ ! ! error . message . match ( new RegExp ( LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE ) ) ||
739
+ ! ! error . message . match ( new RegExp ( NODE_IS_RECOVERING_ERROR_MESSAGE ) )
719
740
) ;
720
741
}
721
742
@@ -724,12 +745,12 @@ const SDAM_RECOVERING_CODES = new Set<number>([
724
745
MONGODB_ERROR_CODES . PrimarySteppedDown ,
725
746
MONGODB_ERROR_CODES . InterruptedAtShutdown ,
726
747
MONGODB_ERROR_CODES . InterruptedDueToReplStateChange ,
727
- MONGODB_ERROR_CODES . NotMasterOrSecondary
748
+ MONGODB_ERROR_CODES . NotPrimaryOrSecondary
728
749
] ) ;
729
750
730
- const SDAM_NOTMASTER_CODES = new Set < number > ( [
731
- MONGODB_ERROR_CODES . NotMaster ,
732
- MONGODB_ERROR_CODES . NotMasterNoSlaveOk ,
751
+ const SDAM_NOTPRIMARY_CODES = new Set < number > ( [
752
+ MONGODB_ERROR_CODES . NotWritablePrimary ,
753
+ MONGODB_ERROR_CODES . NotPrimaryNoSecondaryOk ,
733
754
MONGODB_ERROR_CODES . LegacyNotPrimary
734
755
] ) ;
735
756
@@ -744,20 +765,23 @@ function isRecoveringError(err: MongoError) {
744
765
return SDAM_RECOVERING_CODES . has ( err . code ) ;
745
766
}
746
767
747
- return / n o t m a s t e r o r s e c o n d a r y / . test ( err . message ) || / n o d e i s r e c o v e r i n g / . test ( err . message ) ;
768
+ return (
769
+ new RegExp ( LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE ) . test ( err . message ) ||
770
+ new RegExp ( NODE_IS_RECOVERING_ERROR_MESSAGE ) . test ( err . message )
771
+ ) ;
748
772
}
749
773
750
- function isNotMasterError ( err : MongoError ) {
774
+ function isNotWritablePrimaryError ( err : MongoError ) {
751
775
if ( typeof err . code === 'number' ) {
752
776
// If any error code exists, we ignore the error.message
753
- return SDAM_NOTMASTER_CODES . has ( err . code ) ;
777
+ return SDAM_NOTPRIMARY_CODES . has ( err . code ) ;
754
778
}
755
779
756
780
if ( isRecoveringError ( err ) ) {
757
781
return false ;
758
782
}
759
783
760
- return / n o t m a s t e r / . test ( err . message ) ;
784
+ return new RegExp ( LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE ) . test ( err . message ) ;
761
785
}
762
786
763
787
export function isNodeShuttingDownError ( err : MongoError ) : boolean {
@@ -778,7 +802,7 @@ export function isSDAMUnrecoverableError(error: MongoError): boolean {
778
802
return true ;
779
803
}
780
804
781
- return isRecoveringError ( error ) || isNotMasterError ( error ) ;
805
+ return isRecoveringError ( error ) || isNotWritablePrimaryError ( error ) ;
782
806
}
783
807
784
808
export function isNetworkTimeoutError ( err : MongoError ) : err is MongoNetworkError {
0 commit comments