1
1
'use strict' ;
2
2
3
+ const MONGODB_ERROR_CODES = require ( '../error_codes' ) . MONGODB_ERROR_CODES ;
4
+
3
5
const kErrorLabels = Symbol ( 'errorLabels' ) ;
4
6
5
7
/**
@@ -216,32 +218,32 @@ class MongoWriteConcernError extends MongoError {
216
218
217
219
// see: https://github.com./mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#terms
218
220
const RETRYABLE_ERROR_CODES = new Set ( [
219
- 6 , // HostUnreachable
220
- 7 , // HostNotFound
221
- 89 , // NetworkTimeout
222
- 91 , // ShutdownInProgress
223
- 189 , // PrimarySteppedDown
224
- 9001 , // SocketException
225
- 10107 , // NotMaster
226
- 11600 , // InterruptedAtShutdown
227
- 11602 , // InterruptedDueToReplStateChange
228
- 13435 , // NotMasterNoSlaveOk
229
- 13436 // NotMasterOrSecondary
221
+ MONGODB_ERROR_CODES . HostUnreachable ,
222
+ MONGODB_ERROR_CODES . HostNotFound ,
223
+ MONGODB_ERROR_CODES . NetworkTimeout ,
224
+ MONGODB_ERROR_CODES . ShutdownInProgress ,
225
+ MONGODB_ERROR_CODES . PrimarySteppedDown ,
226
+ MONGODB_ERROR_CODES . SocketException ,
227
+ MONGODB_ERROR_CODES . NotMaster ,
228
+ MONGODB_ERROR_CODES . InterruptedAtShutdown ,
229
+ MONGODB_ERROR_CODES . InterruptedDueToReplStateChange ,
230
+ MONGODB_ERROR_CODES . NotMasterNoSlaveOk ,
231
+ MONGODB_ERROR_CODES . NotMasterOrSecondary
230
232
] ) ;
231
233
232
234
const RETRYABLE_WRITE_ERROR_CODES = new Set ( [
233
- 11600 , // InterruptedAtShutdown
234
- 11602 , // InterruptedDueToReplStateChange
235
- 10107 , // NotMaster
236
- 13435 , // NotMasterNoSlaveOk
237
- 13436 , // NotMasterOrSecondary
238
- 189 , // PrimarySteppedDown
239
- 91 , // ShutdownInProgress
240
- 7 , // HostNotFound
241
- 6 , // HostUnreachable
242
- 89 , // NetworkTimeout
243
- 9001 , // SocketException
244
- 262 // ExceededTimeLimit
235
+ MONGODB_ERROR_CODES . InterruptedAtShutdown ,
236
+ MONGODB_ERROR_CODES . InterruptedDueToReplStateChange ,
237
+ MONGODB_ERROR_CODES . NotMaster ,
238
+ MONGODB_ERROR_CODES . NotMasterNoSlaveOk ,
239
+ MONGODB_ERROR_CODES . NotMasterOrSecondary ,
240
+ MONGODB_ERROR_CODES . PrimarySteppedDown ,
241
+ MONGODB_ERROR_CODES . ShutdownInProgress ,
242
+ MONGODB_ERROR_CODES . HostNotFound ,
243
+ MONGODB_ERROR_CODES . HostUnreachable ,
244
+ MONGODB_ERROR_CODES . NetworkTimeout ,
245
+ MONGODB_ERROR_CODES . SocketException ,
246
+ MONGODB_ERROR_CODES . ExceededTimeLimit
245
247
] ) ;
246
248
247
249
function isRetryableWriteError ( error ) {
@@ -271,41 +273,44 @@ function isRetryableError(error) {
271
273
}
272
274
273
275
const SDAM_RECOVERING_CODES = new Set ( [
274
- 91 , // ShutdownInProgress
275
- 189 , // PrimarySteppedDown
276
- 11600 , // InterruptedAtShutdown
277
- 11602 , // InterruptedDueToReplStateChange
278
- 13436 // NotMasterOrSecondary
276
+ MONGODB_ERROR_CODES . ShutdownInProgress ,
277
+ MONGODB_ERROR_CODES . PrimarySteppedDown ,
278
+ MONGODB_ERROR_CODES . InterruptedAtShutdown ,
279
+ MONGODB_ERROR_CODES . InterruptedDueToReplStateChange ,
280
+ MONGODB_ERROR_CODES . NotMasterOrSecondary
279
281
] ) ;
280
282
281
283
const SDAM_NOTMASTER_CODES = new Set ( [
282
- 10107 , // NotMaster
283
- 13435 // NotMasterNoSlaveOk
284
+ MONGODB_ERROR_CODES . NotMaster ,
285
+ MONGODB_ERROR_CODES . NotMasterNoSlaveOk ,
286
+ MONGODB_ERROR_CODES . LegacyNotPrimary
284
287
] ) ;
285
288
286
289
const SDAM_NODE_SHUTTING_DOWN_ERROR_CODES = new Set ( [
287
- 11600 , // InterruptedAtShutdown
288
- 91 // ShutdownInProgress
290
+ MONGODB_ERROR_CODES . InterruptedAtShutdown ,
291
+ MONGODB_ERROR_CODES . ShutdownInProgress
289
292
] ) ;
290
293
291
294
function isRecoveringError ( err ) {
292
- if ( err . code && SDAM_RECOVERING_CODES . has ( err . code ) ) {
293
- return true ;
295
+ if ( typeof err . code === 'number' ) {
296
+ // If any error code exists, we ignore the error.message
297
+ return SDAM_RECOVERING_CODES . has ( err . code ) ;
294
298
}
295
299
296
- 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 / ) ;
300
+ 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 ) ;
297
301
}
298
302
299
303
function isNotMasterError ( err ) {
300
- if ( err . code && SDAM_NOTMASTER_CODES . has ( err . code ) ) {
301
- return true ;
304
+ if ( typeof err . code === 'number' ) {
305
+ // If any error code exists, we ignore the error.message
306
+ return SDAM_NOTMASTER_CODES . has ( err . code ) ;
302
307
}
303
308
304
309
if ( isRecoveringError ( err ) ) {
305
310
return false ;
306
311
}
307
312
308
- return err . message . match ( / n o t m a s t e r / ) ;
313
+ return / n o t m a s t e r / . test ( err . message ) ;
309
314
}
310
315
311
316
function isNodeShuttingDownError ( err ) {
@@ -316,10 +321,9 @@ function isNodeShuttingDownError(err) {
316
321
* Determines whether SDAM can recover from a given error. If it cannot
317
322
* then the pool will be cleared, and server state will completely reset
318
323
* locally.
319
- *
320
- * @ignore
321
324
* @see https://github.com./mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#not-master-and-node-is-recovering
322
- * @param {MongoError|Error } error
325
+ * @param {MongoError } error
326
+ * @returns {boolean }
323
327
*/
324
328
function isSDAMUnrecoverableError ( error ) {
325
329
// NOTE: null check is here for a strictly pre-CMAP world, a timeout or
@@ -328,11 +332,7 @@ function isSDAMUnrecoverableError(error) {
328
332
return true ;
329
333
}
330
334
331
- if ( isRecoveringError ( error ) || isNotMasterError ( error ) ) {
332
- return true ;
333
- }
334
-
335
- return false ;
335
+ return isRecoveringError ( error ) || isNotMasterError ( error ) ;
336
336
}
337
337
338
338
module . exports = {
0 commit comments