error.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isResumableError = exports.isNetworkTimeoutError = exports.isSDAMUnrecoverableError = exports.isNodeShuttingDownError = exports.isRetryableError = exports.isRetryableWriteError = exports.isRetryableEndTransactionError = exports.MongoWriteConcernError = exports.MongoServerSelectionError = exports.MongoSystemError = exports.MongoMissingDependencyError = exports.MongoMissingCredentialsError = exports.MongoCompatibilityError = exports.MongoInvalidArgumentError = exports.MongoParseError = exports.MongoNetworkTimeoutError = exports.MongoNetworkError = exports.isNetworkErrorBeforeHandshake = exports.MongoTopologyClosedError = exports.MongoCursorExhaustedError = exports.MongoServerClosedError = exports.MongoCursorInUseError = exports.MongoGridFSChunkError = exports.MongoGridFSStreamError = exports.MongoTailableCursorError = exports.MongoChangeStreamError = exports.MongoKerberosError = exports.MongoExpiredSessionError = exports.MongoTransactionError = exports.MongoNotConnectedError = exports.MongoDecompressionError = exports.MongoBatchReExecutionError = exports.MongoRuntimeError = exports.MongoAPIError = exports.MongoDriverError = exports.MongoServerError = exports.MongoError = exports.GET_MORE_RESUMABLE_CODES = exports.MONGODB_ERROR_CODES = exports.NODE_IS_RECOVERING_ERROR_MESSAGE = exports.LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE = exports.LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE = void 0;
  4. /** @internal */
  5. const kErrorLabels = Symbol('errorLabels');
  6. /**
  7. * @internal
  8. * The legacy error message from the server that indicates the node is not a writable primary
  9. * https://github.com/mongodb/specifications/blob/b07c26dc40d04ac20349f989db531c9845fdd755/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#not-writable-primary-and-node-is-recovering
  10. */
  11. exports.LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE = 'not master';
  12. /**
  13. * @internal
  14. * The legacy error message from the server that indicates the node is not a primary or secondary
  15. * https://github.com/mongodb/specifications/blob/b07c26dc40d04ac20349f989db531c9845fdd755/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#not-writable-primary-and-node-is-recovering
  16. */
  17. exports.LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE = 'not master or secondary';
  18. /**
  19. * @internal
  20. * The error message from the server that indicates the node is recovering
  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. exports.NODE_IS_RECOVERING_ERROR_MESSAGE = 'node is recovering';
  24. /** @internal MongoDB Error Codes */
  25. exports.MONGODB_ERROR_CODES = Object.freeze({
  26. HostUnreachable: 6,
  27. HostNotFound: 7,
  28. NetworkTimeout: 89,
  29. ShutdownInProgress: 91,
  30. PrimarySteppedDown: 189,
  31. ExceededTimeLimit: 262,
  32. SocketException: 9001,
  33. NotWritablePrimary: 10107,
  34. InterruptedAtShutdown: 11600,
  35. InterruptedDueToReplStateChange: 11602,
  36. NotPrimaryNoSecondaryOk: 13435,
  37. NotPrimaryOrSecondary: 13436,
  38. StaleShardVersion: 63,
  39. StaleEpoch: 150,
  40. StaleConfig: 13388,
  41. RetryChangeStream: 234,
  42. FailedToSatisfyReadPreference: 133,
  43. CursorNotFound: 43,
  44. LegacyNotPrimary: 10058,
  45. WriteConcernFailed: 64,
  46. NamespaceNotFound: 26,
  47. IllegalOperation: 20,
  48. MaxTimeMSExpired: 50,
  49. UnknownReplWriteConcern: 79,
  50. UnsatisfiableWriteConcern: 100
  51. });
  52. // From spec@https://github.com/mongodb/specifications/blob/f93d78191f3db2898a59013a7ed5650352ef6da8/source/change-streams/change-streams.rst#resumable-error
  53. exports.GET_MORE_RESUMABLE_CODES = new Set([
  54. exports.MONGODB_ERROR_CODES.HostUnreachable,
  55. exports.MONGODB_ERROR_CODES.HostNotFound,
  56. exports.MONGODB_ERROR_CODES.NetworkTimeout,
  57. exports.MONGODB_ERROR_CODES.ShutdownInProgress,
  58. exports.MONGODB_ERROR_CODES.PrimarySteppedDown,
  59. exports.MONGODB_ERROR_CODES.ExceededTimeLimit,
  60. exports.MONGODB_ERROR_CODES.SocketException,
  61. exports.MONGODB_ERROR_CODES.NotWritablePrimary,
  62. exports.MONGODB_ERROR_CODES.InterruptedAtShutdown,
  63. exports.MONGODB_ERROR_CODES.InterruptedDueToReplStateChange,
  64. exports.MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk,
  65. exports.MONGODB_ERROR_CODES.NotPrimaryOrSecondary,
  66. exports.MONGODB_ERROR_CODES.StaleShardVersion,
  67. exports.MONGODB_ERROR_CODES.StaleEpoch,
  68. exports.MONGODB_ERROR_CODES.StaleConfig,
  69. exports.MONGODB_ERROR_CODES.RetryChangeStream,
  70. exports.MONGODB_ERROR_CODES.FailedToSatisfyReadPreference,
  71. exports.MONGODB_ERROR_CODES.CursorNotFound
  72. ]);
  73. /**
  74. * @public
  75. * @category Error
  76. *
  77. * @privateRemarks
  78. * CSFLE has a dependency on this error, it uses the constructor with a string argument
  79. */
  80. class MongoError extends Error {
  81. constructor(message) {
  82. if (message instanceof Error) {
  83. super(message.message);
  84. }
  85. else {
  86. super(message);
  87. }
  88. }
  89. get name() {
  90. return 'MongoError';
  91. }
  92. /** Legacy name for server error responses */
  93. get errmsg() {
  94. return this.message;
  95. }
  96. /**
  97. * Checks the error to see if it has an error label
  98. *
  99. * @param label - The error label to check for
  100. * @returns returns true if the error has the provided error label
  101. */
  102. hasErrorLabel(label) {
  103. if (this[kErrorLabels] == null) {
  104. return false;
  105. }
  106. return this[kErrorLabels].has(label);
  107. }
  108. addErrorLabel(label) {
  109. if (this[kErrorLabels] == null) {
  110. this[kErrorLabels] = new Set();
  111. }
  112. this[kErrorLabels].add(label);
  113. }
  114. get errorLabels() {
  115. return this[kErrorLabels] ? Array.from(this[kErrorLabels]) : [];
  116. }
  117. }
  118. exports.MongoError = MongoError;
  119. /**
  120. * An error coming from the mongo server
  121. *
  122. * @public
  123. * @category Error
  124. */
  125. class MongoServerError extends MongoError {
  126. constructor(message) {
  127. super(message.message || message.errmsg || message.$err || 'n/a');
  128. if (message.errorLabels) {
  129. this[kErrorLabels] = new Set(message.errorLabels);
  130. }
  131. for (const name in message) {
  132. if (name !== 'errorLabels' && name !== 'errmsg' && name !== 'message')
  133. this[name] = message[name];
  134. }
  135. }
  136. get name() {
  137. return 'MongoServerError';
  138. }
  139. }
  140. exports.MongoServerError = MongoServerError;
  141. /**
  142. * An error generated by the driver
  143. *
  144. * @public
  145. * @category Error
  146. */
  147. class MongoDriverError extends MongoError {
  148. constructor(message) {
  149. super(message);
  150. }
  151. get name() {
  152. return 'MongoDriverError';
  153. }
  154. }
  155. exports.MongoDriverError = MongoDriverError;
  156. /**
  157. * An error generated when the driver API is used incorrectly
  158. *
  159. * @privateRemarks
  160. * Should **never** be directly instantiated
  161. *
  162. * @public
  163. * @category Error
  164. */
  165. class MongoAPIError extends MongoDriverError {
  166. constructor(message) {
  167. super(message);
  168. }
  169. get name() {
  170. return 'MongoAPIError';
  171. }
  172. }
  173. exports.MongoAPIError = MongoAPIError;
  174. /**
  175. * An error generated when the driver encounters unexpected input
  176. * or reaches an unexpected/invalid internal state
  177. *
  178. * @privateRemarks
  179. * Should **never** be directly instantiated.
  180. *
  181. * @public
  182. * @category Error
  183. */
  184. class MongoRuntimeError extends MongoDriverError {
  185. constructor(message) {
  186. super(message);
  187. }
  188. get name() {
  189. return 'MongoRuntimeError';
  190. }
  191. }
  192. exports.MongoRuntimeError = MongoRuntimeError;
  193. /**
  194. * An error generated when a batch command is reexecuted after one of the commands in the batch
  195. * has failed
  196. *
  197. * @public
  198. * @category Error
  199. */
  200. class MongoBatchReExecutionError extends MongoAPIError {
  201. constructor(message = 'This batch has already been executed, create new batch to execute') {
  202. super(message);
  203. }
  204. get name() {
  205. return 'MongoBatchReExecutionError';
  206. }
  207. }
  208. exports.MongoBatchReExecutionError = MongoBatchReExecutionError;
  209. /**
  210. * An error generated when the driver fails to decompress
  211. * data received from the server.
  212. *
  213. * @public
  214. * @category Error
  215. */
  216. class MongoDecompressionError extends MongoRuntimeError {
  217. constructor(message) {
  218. super(message);
  219. }
  220. get name() {
  221. return 'MongoDecompressionError';
  222. }
  223. }
  224. exports.MongoDecompressionError = MongoDecompressionError;
  225. /**
  226. * An error thrown when the user attempts to operate on a database or collection through a MongoClient
  227. * that has not yet successfully called the "connect" method
  228. *
  229. * @public
  230. * @category Error
  231. */
  232. class MongoNotConnectedError extends MongoAPIError {
  233. constructor(message) {
  234. super(message);
  235. }
  236. get name() {
  237. return 'MongoNotConnectedError';
  238. }
  239. }
  240. exports.MongoNotConnectedError = MongoNotConnectedError;
  241. /**
  242. * An error generated when the user makes a mistake in the usage of transactions.
  243. * (e.g. attempting to commit a transaction with a readPreference other than primary)
  244. *
  245. * @public
  246. * @category Error
  247. */
  248. class MongoTransactionError extends MongoAPIError {
  249. constructor(message) {
  250. super(message);
  251. }
  252. get name() {
  253. return 'MongoTransactionError';
  254. }
  255. }
  256. exports.MongoTransactionError = MongoTransactionError;
  257. /**
  258. * An error generated when the user attempts to operate
  259. * on a session that has expired or has been closed.
  260. *
  261. * @public
  262. * @category Error
  263. */
  264. class MongoExpiredSessionError extends MongoAPIError {
  265. constructor(message = 'Cannot use a session that has ended') {
  266. super(message);
  267. }
  268. get name() {
  269. return 'MongoExpiredSessionError';
  270. }
  271. }
  272. exports.MongoExpiredSessionError = MongoExpiredSessionError;
  273. /**
  274. * A error generated when the user attempts to authenticate
  275. * via Kerberos, but fails to connect to the Kerberos client.
  276. *
  277. * @public
  278. * @category Error
  279. */
  280. class MongoKerberosError extends MongoRuntimeError {
  281. constructor(message) {
  282. super(message);
  283. }
  284. get name() {
  285. return 'MongoKerberosError';
  286. }
  287. }
  288. exports.MongoKerberosError = MongoKerberosError;
  289. /**
  290. * An error generated when a ChangeStream operation fails to execute.
  291. *
  292. * @public
  293. * @category Error
  294. */
  295. class MongoChangeStreamError extends MongoRuntimeError {
  296. constructor(message) {
  297. super(message);
  298. }
  299. get name() {
  300. return 'MongoChangeStreamError';
  301. }
  302. }
  303. exports.MongoChangeStreamError = MongoChangeStreamError;
  304. /**
  305. * An error thrown when the user calls a function or method not supported on a tailable cursor
  306. *
  307. * @public
  308. * @category Error
  309. */
  310. class MongoTailableCursorError extends MongoAPIError {
  311. constructor(message = 'Tailable cursor does not support this operation') {
  312. super(message);
  313. }
  314. get name() {
  315. return 'MongoTailableCursorError';
  316. }
  317. }
  318. exports.MongoTailableCursorError = MongoTailableCursorError;
  319. /** An error generated when a GridFSStream operation fails to execute.
  320. *
  321. * @public
  322. * @category Error
  323. */
  324. class MongoGridFSStreamError extends MongoRuntimeError {
  325. constructor(message) {
  326. super(message);
  327. }
  328. get name() {
  329. return 'MongoGridFSStreamError';
  330. }
  331. }
  332. exports.MongoGridFSStreamError = MongoGridFSStreamError;
  333. /**
  334. * An error generated when a malformed or invalid chunk is
  335. * encountered when reading from a GridFSStream.
  336. *
  337. * @public
  338. * @category Error
  339. */
  340. class MongoGridFSChunkError extends MongoRuntimeError {
  341. constructor(message) {
  342. super(message);
  343. }
  344. get name() {
  345. return 'MongoGridFSChunkError';
  346. }
  347. }
  348. exports.MongoGridFSChunkError = MongoGridFSChunkError;
  349. /**
  350. * An error thrown when the user attempts to add options to a cursor that has already been
  351. * initialized
  352. *
  353. * @public
  354. * @category Error
  355. */
  356. class MongoCursorInUseError extends MongoAPIError {
  357. constructor(message = 'Cursor is already initialized') {
  358. super(message);
  359. }
  360. get name() {
  361. return 'MongoCursorInUseError';
  362. }
  363. }
  364. exports.MongoCursorInUseError = MongoCursorInUseError;
  365. /**
  366. * An error generated when an attempt is made to operate
  367. * on a closed/closing server.
  368. *
  369. * @public
  370. * @category Error
  371. */
  372. class MongoServerClosedError extends MongoAPIError {
  373. constructor(message = 'Server is closed') {
  374. super(message);
  375. }
  376. get name() {
  377. return 'MongoServerClosedError';
  378. }
  379. }
  380. exports.MongoServerClosedError = MongoServerClosedError;
  381. /**
  382. * An error thrown when an attempt is made to read from a cursor that has been exhausted
  383. *
  384. * @public
  385. * @category Error
  386. */
  387. class MongoCursorExhaustedError extends MongoAPIError {
  388. constructor(message) {
  389. super(message || 'Cursor is exhausted');
  390. }
  391. get name() {
  392. return 'MongoCursorExhaustedError';
  393. }
  394. }
  395. exports.MongoCursorExhaustedError = MongoCursorExhaustedError;
  396. /**
  397. * An error generated when an attempt is made to operate on a
  398. * dropped, or otherwise unavailable, database.
  399. *
  400. * @public
  401. * @category Error
  402. */
  403. class MongoTopologyClosedError extends MongoAPIError {
  404. constructor(message = 'Topology is closed') {
  405. super(message);
  406. }
  407. get name() {
  408. return 'MongoTopologyClosedError';
  409. }
  410. }
  411. exports.MongoTopologyClosedError = MongoTopologyClosedError;
  412. /** @internal */
  413. const kBeforeHandshake = Symbol('beforeHandshake');
  414. function isNetworkErrorBeforeHandshake(err) {
  415. return err[kBeforeHandshake] === true;
  416. }
  417. exports.isNetworkErrorBeforeHandshake = isNetworkErrorBeforeHandshake;
  418. /**
  419. * An error indicating an issue with the network, including TCP errors and timeouts.
  420. * @public
  421. * @category Error
  422. */
  423. class MongoNetworkError extends MongoError {
  424. constructor(message, options) {
  425. super(message);
  426. if (options && typeof options.beforeHandshake === 'boolean') {
  427. this[kBeforeHandshake] = options.beforeHandshake;
  428. }
  429. }
  430. get name() {
  431. return 'MongoNetworkError';
  432. }
  433. }
  434. exports.MongoNetworkError = MongoNetworkError;
  435. /**
  436. * An error indicating a network timeout occurred
  437. * @public
  438. * @category Error
  439. *
  440. * @privateRemarks
  441. * CSFLE has a dependency on this error with an instanceof check
  442. */
  443. class MongoNetworkTimeoutError extends MongoNetworkError {
  444. constructor(message, options) {
  445. super(message, options);
  446. }
  447. get name() {
  448. return 'MongoNetworkTimeoutError';
  449. }
  450. }
  451. exports.MongoNetworkTimeoutError = MongoNetworkTimeoutError;
  452. /**
  453. * An error used when attempting to parse a value (like a connection string)
  454. * @public
  455. * @category Error
  456. */
  457. class MongoParseError extends MongoDriverError {
  458. constructor(message) {
  459. super(message);
  460. }
  461. get name() {
  462. return 'MongoParseError';
  463. }
  464. }
  465. exports.MongoParseError = MongoParseError;
  466. /**
  467. * An error generated when the user supplies malformed or unexpected arguments
  468. * or when a required argument or field is not provided.
  469. *
  470. *
  471. * @public
  472. * @category Error
  473. */
  474. class MongoInvalidArgumentError extends MongoAPIError {
  475. constructor(message) {
  476. super(message);
  477. }
  478. get name() {
  479. return 'MongoInvalidArgumentError';
  480. }
  481. }
  482. exports.MongoInvalidArgumentError = MongoInvalidArgumentError;
  483. /**
  484. * An error generated when a feature that is not enabled or allowed for the current server
  485. * configuration is used
  486. *
  487. *
  488. * @public
  489. * @category Error
  490. */
  491. class MongoCompatibilityError extends MongoAPIError {
  492. constructor(message) {
  493. super(message);
  494. }
  495. get name() {
  496. return 'MongoCompatibilityError';
  497. }
  498. }
  499. exports.MongoCompatibilityError = MongoCompatibilityError;
  500. /**
  501. * An error generated when the user fails to provide authentication credentials before attempting
  502. * to connect to a mongo server instance.
  503. *
  504. *
  505. * @public
  506. * @category Error
  507. */
  508. class MongoMissingCredentialsError extends MongoAPIError {
  509. constructor(message) {
  510. super(message);
  511. }
  512. get name() {
  513. return 'MongoMissingCredentialsError';
  514. }
  515. }
  516. exports.MongoMissingCredentialsError = MongoMissingCredentialsError;
  517. /**
  518. * An error generated when a required module or dependency is not present in the local environment
  519. *
  520. * @public
  521. * @category Error
  522. */
  523. class MongoMissingDependencyError extends MongoAPIError {
  524. constructor(message) {
  525. super(message);
  526. }
  527. get name() {
  528. return 'MongoMissingDependencyError';
  529. }
  530. }
  531. exports.MongoMissingDependencyError = MongoMissingDependencyError;
  532. /**
  533. * An error signifying a general system issue
  534. * @public
  535. * @category Error
  536. */
  537. class MongoSystemError extends MongoError {
  538. constructor(message, reason) {
  539. if (reason && reason.error) {
  540. super(reason.error.message || reason.error);
  541. }
  542. else {
  543. super(message);
  544. }
  545. if (reason) {
  546. this.reason = reason;
  547. }
  548. }
  549. get name() {
  550. return 'MongoSystemError';
  551. }
  552. }
  553. exports.MongoSystemError = MongoSystemError;
  554. /**
  555. * An error signifying a client-side server selection error
  556. * @public
  557. * @category Error
  558. */
  559. class MongoServerSelectionError extends MongoSystemError {
  560. constructor(message, reason) {
  561. super(message, reason);
  562. }
  563. get name() {
  564. return 'MongoServerSelectionError';
  565. }
  566. }
  567. exports.MongoServerSelectionError = MongoServerSelectionError;
  568. function makeWriteConcernResultObject(input) {
  569. const output = Object.assign({}, input);
  570. if (output.ok === 0) {
  571. output.ok = 1;
  572. delete output.errmsg;
  573. delete output.code;
  574. delete output.codeName;
  575. }
  576. return output;
  577. }
  578. /**
  579. * An error thrown when the server reports a writeConcernError
  580. * @public
  581. * @category Error
  582. */
  583. class MongoWriteConcernError extends MongoServerError {
  584. constructor(message, result) {
  585. if (result && Array.isArray(result.errorLabels)) {
  586. message.errorLabels = result.errorLabels;
  587. }
  588. super(message);
  589. this.errInfo = message.errInfo;
  590. if (result != null) {
  591. this.result = makeWriteConcernResultObject(result);
  592. }
  593. }
  594. get name() {
  595. return 'MongoWriteConcernError';
  596. }
  597. }
  598. exports.MongoWriteConcernError = MongoWriteConcernError;
  599. // see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#terms
  600. const RETRYABLE_ERROR_CODES = new Set([
  601. exports.MONGODB_ERROR_CODES.HostUnreachable,
  602. exports.MONGODB_ERROR_CODES.HostNotFound,
  603. exports.MONGODB_ERROR_CODES.NetworkTimeout,
  604. exports.MONGODB_ERROR_CODES.ShutdownInProgress,
  605. exports.MONGODB_ERROR_CODES.PrimarySteppedDown,
  606. exports.MONGODB_ERROR_CODES.SocketException,
  607. exports.MONGODB_ERROR_CODES.NotWritablePrimary,
  608. exports.MONGODB_ERROR_CODES.InterruptedAtShutdown,
  609. exports.MONGODB_ERROR_CODES.InterruptedDueToReplStateChange,
  610. exports.MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk,
  611. exports.MONGODB_ERROR_CODES.NotPrimaryOrSecondary
  612. ]);
  613. const RETRYABLE_WRITE_ERROR_CODES = new Set([
  614. exports.MONGODB_ERROR_CODES.InterruptedAtShutdown,
  615. exports.MONGODB_ERROR_CODES.InterruptedDueToReplStateChange,
  616. exports.MONGODB_ERROR_CODES.NotWritablePrimary,
  617. exports.MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk,
  618. exports.MONGODB_ERROR_CODES.NotPrimaryOrSecondary,
  619. exports.MONGODB_ERROR_CODES.PrimarySteppedDown,
  620. exports.MONGODB_ERROR_CODES.ShutdownInProgress,
  621. exports.MONGODB_ERROR_CODES.HostNotFound,
  622. exports.MONGODB_ERROR_CODES.HostUnreachable,
  623. exports.MONGODB_ERROR_CODES.NetworkTimeout,
  624. exports.MONGODB_ERROR_CODES.SocketException,
  625. exports.MONGODB_ERROR_CODES.ExceededTimeLimit
  626. ]);
  627. function isRetryableEndTransactionError(error) {
  628. return error.hasErrorLabel('RetryableWriteError');
  629. }
  630. exports.isRetryableEndTransactionError = isRetryableEndTransactionError;
  631. function isRetryableWriteError(error) {
  632. var _a, _b, _c;
  633. if (error instanceof MongoWriteConcernError) {
  634. return RETRYABLE_WRITE_ERROR_CODES.has((_c = (_b = (_a = error.result) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : error.code) !== null && _c !== void 0 ? _c : 0);
  635. }
  636. return typeof error.code === 'number' && RETRYABLE_WRITE_ERROR_CODES.has(error.code);
  637. }
  638. exports.isRetryableWriteError = isRetryableWriteError;
  639. /** Determines whether an error is something the driver should attempt to retry */
  640. function isRetryableError(error) {
  641. return (
  642. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  643. (typeof error.code === 'number' && RETRYABLE_ERROR_CODES.has(error.code)) ||
  644. error instanceof MongoNetworkError ||
  645. !!error.message.match(new RegExp(exports.LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE)) ||
  646. !!error.message.match(new RegExp(exports.NODE_IS_RECOVERING_ERROR_MESSAGE)));
  647. }
  648. exports.isRetryableError = isRetryableError;
  649. const SDAM_RECOVERING_CODES = new Set([
  650. exports.MONGODB_ERROR_CODES.ShutdownInProgress,
  651. exports.MONGODB_ERROR_CODES.PrimarySteppedDown,
  652. exports.MONGODB_ERROR_CODES.InterruptedAtShutdown,
  653. exports.MONGODB_ERROR_CODES.InterruptedDueToReplStateChange,
  654. exports.MONGODB_ERROR_CODES.NotPrimaryOrSecondary
  655. ]);
  656. const SDAM_NOTPRIMARY_CODES = new Set([
  657. exports.MONGODB_ERROR_CODES.NotWritablePrimary,
  658. exports.MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk,
  659. exports.MONGODB_ERROR_CODES.LegacyNotPrimary
  660. ]);
  661. const SDAM_NODE_SHUTTING_DOWN_ERROR_CODES = new Set([
  662. exports.MONGODB_ERROR_CODES.InterruptedAtShutdown,
  663. exports.MONGODB_ERROR_CODES.ShutdownInProgress
  664. ]);
  665. function isRecoveringError(err) {
  666. if (typeof err.code === 'number') {
  667. // If any error code exists, we ignore the error.message
  668. return SDAM_RECOVERING_CODES.has(err.code);
  669. }
  670. return (new RegExp(exports.LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE).test(err.message) ||
  671. new RegExp(exports.NODE_IS_RECOVERING_ERROR_MESSAGE).test(err.message));
  672. }
  673. function isNotWritablePrimaryError(err) {
  674. if (typeof err.code === 'number') {
  675. // If any error code exists, we ignore the error.message
  676. return SDAM_NOTPRIMARY_CODES.has(err.code);
  677. }
  678. if (isRecoveringError(err)) {
  679. return false;
  680. }
  681. return new RegExp(exports.LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE).test(err.message);
  682. }
  683. function isNodeShuttingDownError(err) {
  684. return !!(typeof err.code === 'number' && SDAM_NODE_SHUTTING_DOWN_ERROR_CODES.has(err.code));
  685. }
  686. exports.isNodeShuttingDownError = isNodeShuttingDownError;
  687. /**
  688. * Determines whether SDAM can recover from a given error. If it cannot
  689. * then the pool will be cleared, and server state will completely reset
  690. * locally.
  691. *
  692. * @see https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#not-master-and-node-is-recovering
  693. */
  694. function isSDAMUnrecoverableError(error) {
  695. // NOTE: null check is here for a strictly pre-CMAP world, a timeout or
  696. // close event are considered unrecoverable
  697. if (error instanceof MongoParseError || error == null) {
  698. return true;
  699. }
  700. return isRecoveringError(error) || isNotWritablePrimaryError(error);
  701. }
  702. exports.isSDAMUnrecoverableError = isSDAMUnrecoverableError;
  703. function isNetworkTimeoutError(err) {
  704. return !!(err instanceof MongoNetworkError && err.message.match(/timed out/));
  705. }
  706. exports.isNetworkTimeoutError = isNetworkTimeoutError;
  707. // From spec@https://github.com/mongodb/specifications/blob/7a2e93d85935ee4b1046a8d2ad3514c657dc74fa/source/change-streams/change-streams.rst#resumable-error:
  708. //
  709. // An error is considered resumable if it meets any of the following criteria:
  710. // - any error encountered which is not a server error (e.g. a timeout error or network error)
  711. // - any server error response from a getMore command excluding those containing the error label
  712. // NonRetryableChangeStreamError and those containing the following error codes:
  713. // - Interrupted: 11601
  714. // - CappedPositionLost: 136
  715. // - CursorKilled: 237
  716. //
  717. // An error on an aggregate command is not a resumable error. Only errors on a getMore command may be considered resumable errors.
  718. function isResumableError(error, wireVersion) {
  719. if (error instanceof MongoNetworkError) {
  720. return true;
  721. }
  722. if (wireVersion != null && wireVersion >= 9) {
  723. // DRIVERS-1308: For 4.4 drivers running against 4.4 servers, drivers will add a special case to treat the CursorNotFound error code as resumable
  724. if (error && error instanceof MongoError && error.code === 43) {
  725. return true;
  726. }
  727. return error instanceof MongoError && error.hasErrorLabel('ResumableChangeStreamError');
  728. }
  729. if (error && typeof error.code === 'number') {
  730. return exports.GET_MORE_RESUMABLE_CODES.has(error.code);
  731. }
  732. return false;
  733. }
  734. exports.isResumableError = isResumableError;
  735. //# sourceMappingURL=error.js.map