@@ -7,26 +7,55 @@ export type AnyError = MongoError | Error;
77
88const kErrorLabels = Symbol ( 'errorLabels' ) ;
99
10+ /** @internal MongoDB Error Codes */
11+ export const MONGODB_ERROR_CODES = Object . freeze ( {
12+ HostUnreachable : 6 ,
13+ HostNotFound : 7 ,
14+ NetworkTimeout : 89 ,
15+ ShutdownInProgress : 91 ,
16+ PrimarySteppedDown : 189 ,
17+ ExceededTimeLimit : 262 ,
18+ SocketException : 9001 ,
19+ NotMaster : 10107 ,
20+ InterruptedAtShutdown : 11600 ,
21+ InterruptedDueToReplStateChange : 11602 ,
22+ NotMasterNoSlaveOk : 13435 ,
23+ NotMasterOrSecondary : 13436 ,
24+ StaleShardVersion : 63 ,
25+ StaleEpoch : 150 ,
26+ StaleConfig : 13388 ,
27+ RetryChangeStream : 234 ,
28+ FailedToSatisfyReadPreference : 133 ,
29+ CursorNotFound : 43 ,
30+ LegacyNotPrimary : 10058 ,
31+ WriteConcernFailed : 64 ,
32+ NamespaceNotFound : 26 ,
33+ IllegalOperation : 20 ,
34+ MaxTimeMSExpired : 50 ,
35+ UnknownReplWriteConcern : 79 ,
36+ UnsatisfiableWriteConcern : 100
37+ } as const ) ;
38+
1039// From spec@https ://github.com/mongodb/specifications/blob/f93d78191f3db2898a59013a7ed5650352ef6da8/source/change-streams/change-streams.rst#resumable-error
11- export const GET_MORE_RESUMABLE_CODES = new Set ( [
12- 6 , // HostUnreachable
13- 7 , // HostNotFound
14- 89 , // NetworkTimeout
15- 91 , // ShutdownInProgress
16- 189 , // PrimarySteppedDown
17- 262 , // ExceededTimeLimit
18- 9001 , // SocketException
19- 10107 , // NotMaster
20- 11600 , // InterruptedAtShutdown
21- 11602 , // InterruptedDueToReplStateChange
22- 13435 , // NotMasterNoSlaveOk
23- 13436 , // NotMasterOrSecondary
24- 63 , // StaleShardVersion
25- 150 , // StaleEpoch
26- 13388 , // StaleConfig
27- 234 , // RetryChangeStream
28- 133 , // FailedToSatisfyReadPreference
29- 43 // CursorNotFound
40+ export const GET_MORE_RESUMABLE_CODES = new Set < number > ( [
41+ MONGODB_ERROR_CODES . HostUnreachable ,
42+ MONGODB_ERROR_CODES . HostNotFound ,
43+ MONGODB_ERROR_CODES . NetworkTimeout ,
44+ MONGODB_ERROR_CODES . ShutdownInProgress ,
45+ MONGODB_ERROR_CODES . PrimarySteppedDown ,
46+ MONGODB_ERROR_CODES . ExceededTimeLimit ,
47+ MONGODB_ERROR_CODES . SocketException ,
48+ MONGODB_ERROR_CODES . NotMaster ,
49+ MONGODB_ERROR_CODES . InterruptedAtShutdown ,
50+ MONGODB_ERROR_CODES . InterruptedDueToReplStateChange ,
51+ MONGODB_ERROR_CODES . NotMasterNoSlaveOk ,
52+ MONGODB_ERROR_CODES . NotMasterOrSecondary ,
53+ MONGODB_ERROR_CODES . StaleShardVersion ,
54+ MONGODB_ERROR_CODES . StaleEpoch ,
55+ MONGODB_ERROR_CODES . StaleConfig ,
56+ MONGODB_ERROR_CODES . RetryChangeStream ,
57+ MONGODB_ERROR_CODES . FailedToSatisfyReadPreference ,
58+ MONGODB_ERROR_CODES . CursorNotFound
3059] ) ;
3160
3261/** @public */
@@ -244,33 +273,33 @@ export class MongoWriteConcernError extends MongoError {
244273}
245274
246275// see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#terms
247- const RETRYABLE_ERROR_CODES = new Set ( [
248- 6 , // HostUnreachable
249- 7 , // HostNotFound
250- 89 , // NetworkTimeout
251- 91 , // ShutdownInProgress
252- 189 , // PrimarySteppedDown
253- 9001 , // SocketException
254- 10107 , // NotMaster
255- 11600 , // InterruptedAtShutdown
256- 11602 , // InterruptedDueToReplStateChange
257- 13435 , // NotMasterNoSlaveOk
258- 13436 // NotMasterOrSecondary
276+ const RETRYABLE_ERROR_CODES = new Set < number > ( [
277+ MONGODB_ERROR_CODES . HostUnreachable ,
278+ MONGODB_ERROR_CODES . HostNotFound ,
279+ MONGODB_ERROR_CODES . NetworkTimeout ,
280+ MONGODB_ERROR_CODES . ShutdownInProgress ,
281+ MONGODB_ERROR_CODES . PrimarySteppedDown ,
282+ MONGODB_ERROR_CODES . SocketException ,
283+ MONGODB_ERROR_CODES . NotMaster ,
284+ MONGODB_ERROR_CODES . InterruptedAtShutdown ,
285+ MONGODB_ERROR_CODES . InterruptedDueToReplStateChange ,
286+ MONGODB_ERROR_CODES . NotMasterNoSlaveOk ,
287+ MONGODB_ERROR_CODES . NotMasterOrSecondary
259288] ) ;
260289
261- const RETRYABLE_WRITE_ERROR_CODES = new Set ( [
262- 11600 , // InterruptedAtShutdown
263- 11602 , // InterruptedDueToReplStateChange
264- 10107 , // NotMaster
265- 13435 , // NotMasterNoSlaveOk
266- 13436 , // NotMasterOrSecondary
267- 189 , // PrimarySteppedDown
268- 91 , // ShutdownInProgress
269- 7 , // HostNotFound
270- 6 , // HostUnreachable
271- 89 , // NetworkTimeout
272- 9001 , // SocketException
273- 262 // ExceededTimeLimit
290+ const RETRYABLE_WRITE_ERROR_CODES = new Set < number > ( [
291+ MONGODB_ERROR_CODES . InterruptedAtShutdown ,
292+ MONGODB_ERROR_CODES . InterruptedDueToReplStateChange ,
293+ MONGODB_ERROR_CODES . NotMaster ,
294+ MONGODB_ERROR_CODES . NotMasterNoSlaveOk ,
295+ MONGODB_ERROR_CODES . NotMasterOrSecondary ,
296+ MONGODB_ERROR_CODES . PrimarySteppedDown ,
297+ MONGODB_ERROR_CODES . ShutdownInProgress ,
298+ MONGODB_ERROR_CODES . HostNotFound ,
299+ MONGODB_ERROR_CODES . HostUnreachable ,
300+ MONGODB_ERROR_CODES . NetworkTimeout ,
301+ MONGODB_ERROR_CODES . SocketException ,
302+ MONGODB_ERROR_CODES . ExceededTimeLimit
274303] ) ;
275304
276305export function isRetryableWriteError ( error : MongoError ) : boolean {
@@ -291,42 +320,45 @@ export function isRetryableError(error: MongoError): boolean {
291320 ) ;
292321}
293322
294- const SDAM_RECOVERING_CODES = new Set ( [
295- 91 , // ShutdownInProgress
296- 189 , // PrimarySteppedDown
297- 11600 , // InterruptedAtShutdown
298- 11602 , // InterruptedDueToReplStateChange
299- 13436 // NotMasterOrSecondary
323+ const SDAM_RECOVERING_CODES = new Set < number > ( [
324+ MONGODB_ERROR_CODES . ShutdownInProgress ,
325+ MONGODB_ERROR_CODES . PrimarySteppedDown ,
326+ MONGODB_ERROR_CODES . InterruptedAtShutdown ,
327+ MONGODB_ERROR_CODES . InterruptedDueToReplStateChange ,
328+ MONGODB_ERROR_CODES . NotMasterOrSecondary
300329] ) ;
301330
302- const SDAM_NOTMASTER_CODES = new Set ( [
303- 10107 , // NotMaster
304- 13435 // NotMasterNoSlaveOk
331+ const SDAM_NOTMASTER_CODES = new Set < number > ( [
332+ MONGODB_ERROR_CODES . NotMaster ,
333+ MONGODB_ERROR_CODES . NotMasterNoSlaveOk ,
334+ MONGODB_ERROR_CODES . LegacyNotPrimary
305335] ) ;
306336
307- const SDAM_NODE_SHUTTING_DOWN_ERROR_CODES = new Set ( [
308- 11600 , // InterruptedAtShutdown
309- 91 // ShutdownInProgress
337+ const SDAM_NODE_SHUTTING_DOWN_ERROR_CODES = new Set < number > ( [
338+ MONGODB_ERROR_CODES . InterruptedAtShutdown ,
339+ MONGODB_ERROR_CODES . ShutdownInProgress
310340] ) ;
311341
312342function isRecoveringError ( err : MongoError ) {
313- if ( err . code && SDAM_RECOVERING_CODES . has ( err . code ) ) {
314- return true ;
343+ if ( typeof err . code !== 'undefined' ) {
344+ // If any error code exists, we ignore the error.message
345+ return SDAM_RECOVERING_CODES . has ( err . code ) ;
315346 }
316347
317- return err . message . match ( / n o t m a s t e r o r s e c o n d a r y / ) || err . message . match ( / n o d e i s r e c o v e r i n g / ) ;
348+ 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 ) ;
318349}
319350
320351function isNotMasterError ( err : MongoError ) {
321- if ( err . code && SDAM_NOTMASTER_CODES . has ( err . code ) ) {
322- return true ;
352+ if ( typeof err . code !== 'undefined' ) {
353+ // If any error code exists, we ignore the error.message
354+ return SDAM_NOTMASTER_CODES . has ( err . code ) ;
323355 }
324356
325357 if ( isRecoveringError ( err ) ) {
326358 return false ;
327359 }
328360
329- return err . message . match ( / n o t m a s t e r / ) ;
361+ return / n o t m a s t e r / . test ( err . message ) ;
330362}
331363
332364export function isNodeShuttingDownError ( err : MongoError ) : boolean {
@@ -347,6 +379,9 @@ export function isSDAMUnrecoverableError(error: MongoError): boolean {
347379 return true ;
348380 }
349381
382+ if ( typeof error . code !== 'undefined' ) {
383+ return isRecoveringError ( error ) || isNotMasterError ( error ) ;
384+ }
350385 if ( isRecoveringError ( error ) || isNotMasterError ( error ) ) {
351386 return true ;
352387 }
0 commit comments