MCPcopy
hub / github.com/mongodb/node-mongodb-native / needsRetryableWriteLabel

Function needsRetryableWriteLabel

src/error.ts:1397–1444  ·  view source on GitHub ↗
(
  error: Error,
  maxWireVersion: number,
  serverType: ServerType
)

Source from the content-addressed store, hash-verified

1395const RETRYABLE_WRITE_ERROR_CODES = RETRYABLE_READ_ERROR_CODES;
1396
1397export function needsRetryableWriteLabel(
1398 error: Error,
1399 maxWireVersion: number,
1400 serverType: ServerType
1401): boolean {
1402 // pre-4.4 server, then the driver adds an error label for every valid case
1403 // execute operation will only inspect the label, code/message logic is handled here
1404 if (error instanceof MongoNetworkError) {
1405 return true;
1406 }
1407
1408 if (error instanceof MongoError) {
1409 if (
1410 (maxWireVersion >= 9 || isRetryableWriteError(error)) &&
1411 !error.hasErrorLabel(MongoErrorLabel.HandshakeError)
1412 ) {
1413 // If we already have the error label no need to add it again. 4.4+ servers add the label.
1414 // In the case where we have a handshake error, need to fall down to the logic checking
1415 // the codes.
1416 return false;
1417 }
1418 }
1419
1420 if (error instanceof MongoWriteConcernError) {
1421 if (serverType === 'Mongos' && maxWireVersion < 9) {
1422 // use original top-level code from server response
1423 return RETRYABLE_WRITE_ERROR_CODES.has(error.result.code ?? 0);
1424 }
1425 const code = error.result.writeConcernError.code ?? Number(error.code);
1426 return RETRYABLE_WRITE_ERROR_CODES.has(Number.isNaN(code) ? 0 : code);
1427 }
1428
1429 if (error instanceof MongoError) {
1430 return RETRYABLE_WRITE_ERROR_CODES.has(Number(error.code));
1431 }
1432
1433 const isNotWritablePrimaryError = LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE.test(error.message);
1434 if (isNotWritablePrimaryError) {
1435 return true;
1436 }
1437
1438 const isNodeIsRecoveringError = NODE_IS_RECOVERING_ERROR_MESSAGE.test(error.message);
1439 if (isNodeIsRecoveringError) {
1440 return true;
1441 }
1442
1443 return false;
1444}
1445
1446export function isRetryableWriteError(error: MongoError): boolean {
1447 return (

Callers 3

performInitialHandshakeFunction · 0.90
decorateCommandErrorMethod · 0.90
error.test.tsFile · 0.85

Calls 4

isRetryableWriteErrorFunction · 0.85
testMethod · 0.80
hasErrorLabelMethod · 0.45
hasMethod · 0.45

Tested by

no test coverage detected