error.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.MongoWriteConcernError = exports.MongoServerSelectionError = exports.MongoSystemError = exports.MongoMissingDependencyError = exports.MongoMissingCredentialsError = exports.MongoCompatibilityError = exports.MongoInvalidArgumentError = exports.MongoParseError = exports.MongoNetworkTimeoutError = exports.MongoNetworkError = exports.MongoClientClosedError = exports.MongoTopologyClosedError = exports.MongoCursorExhaustedError = exports.MongoServerClosedError = exports.MongoCursorInUseError = exports.MongoOperationTimeoutError = exports.MongoUnexpectedServerResponseError = exports.MongoGridFSChunkError = exports.MongoGridFSStreamError = exports.MongoTailableCursorError = exports.MongoChangeStreamError = exports.MongoClientBulkWriteExecutionError = exports.MongoClientBulkWriteCursorError = exports.MongoClientBulkWriteError = exports.MongoGCPError = exports.MongoAzureError = exports.MongoOIDCError = exports.MongoAWSError = exports.MongoKerberosError = exports.MongoExpiredSessionError = exports.MongoTransactionError = exports.MongoNotConnectedError = exports.MongoDecompressionError = exports.MongoBatchReExecutionError = exports.MongoStalePrimaryError = exports.MongoRuntimeError = exports.MongoAPIError = exports.MongoDriverError = exports.MongoServerError = exports.MongoError = exports.MongoErrorLabel = 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. exports.needsRetryableWriteLabel = needsRetryableWriteLabel;
  5. exports.isRetryableWriteError = isRetryableWriteError;
  6. exports.isRetryableReadError = isRetryableReadError;
  7. exports.isNodeShuttingDownError = isNodeShuttingDownError;
  8. exports.isSDAMUnrecoverableError = isSDAMUnrecoverableError;
  9. exports.isNetworkTimeoutError = isNetworkTimeoutError;
  10. exports.isResumableError = isResumableError;
  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/921232976f9913cf17415b5ef937ee772e45e6ae/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md#not-writable-primary-and-node-is-recovering
  15. */
  16. exports.LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE = new RegExp('not master', 'i');
  17. /**
  18. * @internal
  19. * The legacy error message from the server that indicates the node is not a primary or secondary
  20. * https://github.com/mongodb/specifications/blob/921232976f9913cf17415b5ef937ee772e45e6ae/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md#not-writable-primary-and-node-is-recovering
  21. */
  22. exports.LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE = new RegExp('not master or secondary', 'i');
  23. /**
  24. * @internal
  25. * The error message from the server that indicates the node is recovering
  26. * https://github.com/mongodb/specifications/blob/921232976f9913cf17415b5ef937ee772e45e6ae/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md#not-writable-primary-and-node-is-recovering
  27. */
  28. exports.NODE_IS_RECOVERING_ERROR_MESSAGE = new RegExp('node is recovering', 'i');
  29. /** @internal MongoDB Error Codes */
  30. exports.MONGODB_ERROR_CODES = Object.freeze({
  31. HostUnreachable: 6,
  32. HostNotFound: 7,
  33. AuthenticationFailed: 18,
  34. NetworkTimeout: 89,
  35. ShutdownInProgress: 91,
  36. PrimarySteppedDown: 189,
  37. ExceededTimeLimit: 262,
  38. SocketException: 9001,
  39. NotWritablePrimary: 10107,
  40. InterruptedAtShutdown: 11600,
  41. InterruptedDueToReplStateChange: 11602,
  42. NotPrimaryNoSecondaryOk: 13435,
  43. NotPrimaryOrSecondary: 13436,
  44. StaleShardVersion: 63,
  45. StaleEpoch: 150,
  46. StaleConfig: 13388,
  47. RetryChangeStream: 234,
  48. FailedToSatisfyReadPreference: 133,
  49. CursorNotFound: 43,
  50. LegacyNotPrimary: 10058,
  51. // WriteConcernTimeout is WriteConcernFailed on pre-8.1 servers
  52. WriteConcernTimeout: 64,
  53. NamespaceNotFound: 26,
  54. IllegalOperation: 20,
  55. MaxTimeMSExpired: 50,
  56. UnknownReplWriteConcern: 79,
  57. UnsatisfiableWriteConcern: 100,
  58. Reauthenticate: 391,
  59. ReadConcernMajorityNotAvailableYet: 134
  60. });
  61. // From spec https://github.com/mongodb/specifications/blob/921232976f9913cf17415b5ef937ee772e45e6ae/source/change-streams/change-streams.md#resumable-error
  62. exports.GET_MORE_RESUMABLE_CODES = new Set([
  63. exports.MONGODB_ERROR_CODES.HostUnreachable,
  64. exports.MONGODB_ERROR_CODES.HostNotFound,
  65. exports.MONGODB_ERROR_CODES.NetworkTimeout,
  66. exports.MONGODB_ERROR_CODES.ShutdownInProgress,
  67. exports.MONGODB_ERROR_CODES.PrimarySteppedDown,
  68. exports.MONGODB_ERROR_CODES.ExceededTimeLimit,
  69. exports.MONGODB_ERROR_CODES.SocketException,
  70. exports.MONGODB_ERROR_CODES.NotWritablePrimary,
  71. exports.MONGODB_ERROR_CODES.InterruptedAtShutdown,
  72. exports.MONGODB_ERROR_CODES.InterruptedDueToReplStateChange,
  73. exports.MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk,
  74. exports.MONGODB_ERROR_CODES.NotPrimaryOrSecondary,
  75. exports.MONGODB_ERROR_CODES.StaleShardVersion,
  76. exports.MONGODB_ERROR_CODES.StaleEpoch,
  77. exports.MONGODB_ERROR_CODES.StaleConfig,
  78. exports.MONGODB_ERROR_CODES.RetryChangeStream,
  79. exports.MONGODB_ERROR_CODES.FailedToSatisfyReadPreference,
  80. exports.MONGODB_ERROR_CODES.CursorNotFound
  81. ]);
  82. /** @public */
  83. exports.MongoErrorLabel = Object.freeze({
  84. RetryableWriteError: 'RetryableWriteError',
  85. TransientTransactionError: 'TransientTransactionError',
  86. UnknownTransactionCommitResult: 'UnknownTransactionCommitResult',
  87. ResumableChangeStreamError: 'ResumableChangeStreamError',
  88. HandshakeError: 'HandshakeError',
  89. ResetPool: 'ResetPool',
  90. PoolRequestedRetry: 'PoolRequestedRetry',
  91. InterruptInUseConnections: 'InterruptInUseConnections',
  92. NoWritesPerformed: 'NoWritesPerformed'
  93. });
  94. function isAggregateError(e) {
  95. return e != null && typeof e === 'object' && 'errors' in e && Array.isArray(e.errors);
  96. }
  97. /**
  98. * @public
  99. * @category Error
  100. *
  101. * @privateRemarks
  102. * mongodb-client-encryption has a dependency on this error, it uses the constructor with a string argument
  103. */
  104. class MongoError extends Error {
  105. get errorLabels() {
  106. return Array.from(this.errorLabelSet);
  107. }
  108. /**
  109. * **Do not use this constructor!**
  110. *
  111. * Meant for internal use only.
  112. *
  113. * @remarks
  114. * This class is only meant to be constructed within the driver. This constructor is
  115. * not subject to semantic versioning compatibility guarantees and may change at any time.
  116. *
  117. * @public
  118. **/
  119. constructor(message, options) {
  120. super(message, options);
  121. /** @internal */
  122. this.errorLabelSet = new Set();
  123. }
  124. /** @internal */
  125. static buildErrorMessage(e) {
  126. if (typeof e === 'string') {
  127. return e;
  128. }
  129. if (isAggregateError(e) && e.message.length === 0) {
  130. return e.errors.length === 0
  131. ? 'AggregateError has an empty errors array. Please check the `cause` property for more information.'
  132. : e.errors.map(({ message }) => message).join(', ');
  133. }
  134. return e != null && typeof e === 'object' && 'message' in e && typeof e.message === 'string'
  135. ? e.message
  136. : 'empty error message';
  137. }
  138. get name() {
  139. return 'MongoError';
  140. }
  141. /** Legacy name for server error responses */
  142. get errmsg() {
  143. return this.message;
  144. }
  145. /**
  146. * Checks the error to see if it has an error label
  147. *
  148. * @param label - The error label to check for
  149. * @returns returns true if the error has the provided error label
  150. */
  151. hasErrorLabel(label) {
  152. return this.errorLabelSet.has(label);
  153. }
  154. addErrorLabel(label) {
  155. this.errorLabelSet.add(label);
  156. }
  157. }
  158. exports.MongoError = MongoError;
  159. /**
  160. * An error coming from the mongo server
  161. *
  162. * @public
  163. * @category Error
  164. */
  165. class MongoServerError extends MongoError {
  166. /**
  167. * **Do not use this constructor!**
  168. *
  169. * Meant for internal use only.
  170. *
  171. * @remarks
  172. * This class is only meant to be constructed within the driver. This constructor is
  173. * not subject to semantic versioning compatibility guarantees and may change at any time.
  174. *
  175. * @public
  176. **/
  177. constructor(message) {
  178. super(message.message || message.errmsg || message.$err || 'n/a');
  179. if (message.errorLabels) {
  180. for (const label of message.errorLabels)
  181. this.addErrorLabel(label);
  182. }
  183. this.errorResponse = message;
  184. for (const name in message) {
  185. if (name !== 'errorLabels' &&
  186. name !== 'errmsg' &&
  187. name !== 'message' &&
  188. name !== 'errorResponse') {
  189. this[name] = message[name];
  190. }
  191. }
  192. }
  193. get name() {
  194. return 'MongoServerError';
  195. }
  196. }
  197. exports.MongoServerError = MongoServerError;
  198. /**
  199. * An error generated by the driver
  200. *
  201. * @public
  202. * @category Error
  203. */
  204. class MongoDriverError extends MongoError {
  205. /**
  206. * **Do not use this constructor!**
  207. *
  208. * Meant for internal use only.
  209. *
  210. * @remarks
  211. * This class is only meant to be constructed within the driver. This constructor is
  212. * not subject to semantic versioning compatibility guarantees and may change at any time.
  213. *
  214. * @public
  215. **/
  216. constructor(message, options) {
  217. super(message, options);
  218. }
  219. get name() {
  220. return 'MongoDriverError';
  221. }
  222. }
  223. exports.MongoDriverError = MongoDriverError;
  224. /**
  225. * An error generated when the driver API is used incorrectly
  226. *
  227. * @privateRemarks
  228. * Should **never** be directly instantiated
  229. *
  230. * @public
  231. * @category Error
  232. */
  233. class MongoAPIError extends MongoDriverError {
  234. /**
  235. * **Do not use this constructor!**
  236. *
  237. * Meant for internal use only.
  238. *
  239. * @remarks
  240. * This class is only meant to be constructed within the driver. This constructor is
  241. * not subject to semantic versioning compatibility guarantees and may change at any time.
  242. *
  243. * @public
  244. **/
  245. constructor(message, options) {
  246. super(message, options);
  247. }
  248. get name() {
  249. return 'MongoAPIError';
  250. }
  251. }
  252. exports.MongoAPIError = MongoAPIError;
  253. /**
  254. * An error generated when the driver encounters unexpected input
  255. * or reaches an unexpected/invalid internal state.
  256. *
  257. * @privateRemarks
  258. * Should **never** be directly instantiated.
  259. *
  260. * @public
  261. * @category Error
  262. */
  263. class MongoRuntimeError extends MongoDriverError {
  264. /**
  265. * **Do not use this constructor!**
  266. *
  267. * Meant for internal use only.
  268. *
  269. * @remarks
  270. * This class is only meant to be constructed within the driver. This constructor is
  271. * not subject to semantic versioning compatibility guarantees and may change at any time.
  272. *
  273. * @public
  274. **/
  275. constructor(message, options) {
  276. super(message, options);
  277. }
  278. get name() {
  279. return 'MongoRuntimeError';
  280. }
  281. }
  282. exports.MongoRuntimeError = MongoRuntimeError;
  283. /**
  284. * An error generated when a primary server is marked stale, never directly thrown
  285. *
  286. * @public
  287. * @category Error
  288. */
  289. class MongoStalePrimaryError extends MongoRuntimeError {
  290. /**
  291. * **Do not use this constructor!**
  292. *
  293. * Meant for internal use only.
  294. *
  295. * @remarks
  296. * This class is only meant to be constructed within the driver. This constructor is
  297. * not subject to semantic versioning compatibility guarantees and may change at any time.
  298. *
  299. * @public
  300. **/
  301. constructor(message, options) {
  302. super(message, options);
  303. }
  304. get name() {
  305. return 'MongoStalePrimaryError';
  306. }
  307. }
  308. exports.MongoStalePrimaryError = MongoStalePrimaryError;
  309. /**
  310. * An error generated when a batch command is re-executed after one of the commands in the batch
  311. * has failed
  312. *
  313. * @public
  314. * @category Error
  315. */
  316. class MongoBatchReExecutionError extends MongoAPIError {
  317. /**
  318. * **Do not use this constructor!**
  319. *
  320. * Meant for internal use only.
  321. *
  322. * @remarks
  323. * This class is only meant to be constructed within the driver. This constructor is
  324. * not subject to semantic versioning compatibility guarantees and may change at any time.
  325. *
  326. * @public
  327. **/
  328. constructor(message = 'This batch has already been executed, create new batch to execute') {
  329. super(message);
  330. }
  331. get name() {
  332. return 'MongoBatchReExecutionError';
  333. }
  334. }
  335. exports.MongoBatchReExecutionError = MongoBatchReExecutionError;
  336. /**
  337. * An error generated when the driver fails to decompress
  338. * data received from the server.
  339. *
  340. * @public
  341. * @category Error
  342. */
  343. class MongoDecompressionError extends MongoRuntimeError {
  344. /**
  345. * **Do not use this constructor!**
  346. *
  347. * Meant for internal use only.
  348. *
  349. * @remarks
  350. * This class is only meant to be constructed within the driver. This constructor is
  351. * not subject to semantic versioning compatibility guarantees and may change at any time.
  352. *
  353. * @public
  354. **/
  355. constructor(message) {
  356. super(message);
  357. }
  358. get name() {
  359. return 'MongoDecompressionError';
  360. }
  361. }
  362. exports.MongoDecompressionError = MongoDecompressionError;
  363. /**
  364. * An error thrown when the user attempts to operate on a database or collection through a MongoClient
  365. * that has not yet successfully called the "connect" method
  366. *
  367. * @public
  368. * @category Error
  369. */
  370. class MongoNotConnectedError extends MongoAPIError {
  371. /**
  372. * **Do not use this constructor!**
  373. *
  374. * Meant for internal use only.
  375. *
  376. * @remarks
  377. * This class is only meant to be constructed within the driver. This constructor is
  378. * not subject to semantic versioning compatibility guarantees and may change at any time.
  379. *
  380. * @public
  381. **/
  382. constructor(message) {
  383. super(message);
  384. }
  385. get name() {
  386. return 'MongoNotConnectedError';
  387. }
  388. }
  389. exports.MongoNotConnectedError = MongoNotConnectedError;
  390. /**
  391. * An error generated when the user makes a mistake in the usage of transactions.
  392. * (e.g. attempting to commit a transaction with a readPreference other than primary)
  393. *
  394. * @public
  395. * @category Error
  396. */
  397. class MongoTransactionError extends MongoAPIError {
  398. /**
  399. * **Do not use this constructor!**
  400. *
  401. * Meant for internal use only.
  402. *
  403. * @remarks
  404. * This class is only meant to be constructed within the driver. This constructor is
  405. * not subject to semantic versioning compatibility guarantees and may change at any time.
  406. *
  407. * @public
  408. **/
  409. constructor(message) {
  410. super(message);
  411. }
  412. get name() {
  413. return 'MongoTransactionError';
  414. }
  415. }
  416. exports.MongoTransactionError = MongoTransactionError;
  417. /**
  418. * An error generated when the user attempts to operate
  419. * on a session that has expired or has been closed.
  420. *
  421. * @public
  422. * @category Error
  423. */
  424. class MongoExpiredSessionError extends MongoAPIError {
  425. /**
  426. * **Do not use this constructor!**
  427. *
  428. * Meant for internal use only.
  429. *
  430. * @remarks
  431. * This class is only meant to be constructed within the driver. This constructor is
  432. * not subject to semantic versioning compatibility guarantees and may change at any time.
  433. *
  434. * @public
  435. **/
  436. constructor(message = 'Cannot use a session that has ended') {
  437. super(message);
  438. }
  439. get name() {
  440. return 'MongoExpiredSessionError';
  441. }
  442. }
  443. exports.MongoExpiredSessionError = MongoExpiredSessionError;
  444. /**
  445. * A error generated when the user attempts to authenticate
  446. * via Kerberos, but fails to connect to the Kerberos client.
  447. *
  448. * @public
  449. * @category Error
  450. */
  451. class MongoKerberosError extends MongoRuntimeError {
  452. /**
  453. * **Do not use this constructor!**
  454. *
  455. * Meant for internal use only.
  456. *
  457. * @remarks
  458. * This class is only meant to be constructed within the driver. This constructor is
  459. * not subject to semantic versioning compatibility guarantees and may change at any time.
  460. *
  461. * @public
  462. **/
  463. constructor(message) {
  464. super(message);
  465. }
  466. get name() {
  467. return 'MongoKerberosError';
  468. }
  469. }
  470. exports.MongoKerberosError = MongoKerberosError;
  471. /**
  472. * A error generated when the user attempts to authenticate
  473. * via AWS, but fails
  474. *
  475. * @public
  476. * @category Error
  477. */
  478. class MongoAWSError extends MongoRuntimeError {
  479. /**
  480. * **Do not use this constructor!**
  481. *
  482. * Meant for internal use only.
  483. *
  484. * @remarks
  485. * This class is only meant to be constructed within the driver. This constructor is
  486. * not subject to semantic versioning compatibility guarantees and may change at any time.
  487. *
  488. * @public
  489. **/
  490. constructor(message, options) {
  491. super(message, options);
  492. }
  493. get name() {
  494. return 'MongoAWSError';
  495. }
  496. }
  497. exports.MongoAWSError = MongoAWSError;
  498. /**
  499. * A error generated when the user attempts to authenticate
  500. * via OIDC callbacks, but fails.
  501. *
  502. * @public
  503. * @category Error
  504. */
  505. class MongoOIDCError extends MongoRuntimeError {
  506. /**
  507. * **Do not use this constructor!**
  508. *
  509. * Meant for internal use only.
  510. *
  511. * @remarks
  512. * This class is only meant to be constructed within the driver. This constructor is
  513. * not subject to semantic versioning compatibility guarantees and may change at any time.
  514. *
  515. * @public
  516. **/
  517. constructor(message) {
  518. super(message);
  519. }
  520. get name() {
  521. return 'MongoOIDCError';
  522. }
  523. }
  524. exports.MongoOIDCError = MongoOIDCError;
  525. /**
  526. * A error generated when the user attempts to authenticate
  527. * via Azure, but fails.
  528. *
  529. * @public
  530. * @category Error
  531. */
  532. class MongoAzureError extends MongoOIDCError {
  533. /**
  534. * **Do not use this constructor!**
  535. *
  536. * Meant for internal use only.
  537. *
  538. * @remarks
  539. * This class is only meant to be constructed within the driver. This constructor is
  540. * not subject to semantic versioning compatibility guarantees and may change at any time.
  541. *
  542. * @public
  543. **/
  544. constructor(message) {
  545. super(message);
  546. }
  547. get name() {
  548. return 'MongoAzureError';
  549. }
  550. }
  551. exports.MongoAzureError = MongoAzureError;
  552. /**
  553. * A error generated when the user attempts to authenticate
  554. * via GCP, but fails.
  555. *
  556. * @public
  557. * @category Error
  558. */
  559. class MongoGCPError extends MongoOIDCError {
  560. /**
  561. * **Do not use this constructor!**
  562. *
  563. * Meant for internal use only.
  564. *
  565. * @remarks
  566. * This class is only meant to be constructed within the driver. This constructor is
  567. * not subject to semantic versioning compatibility guarantees and may change at any time.
  568. *
  569. * @public
  570. **/
  571. constructor(message) {
  572. super(message);
  573. }
  574. get name() {
  575. return 'MongoGCPError';
  576. }
  577. }
  578. exports.MongoGCPError = MongoGCPError;
  579. /**
  580. * An error indicating that an error occurred when executing the bulk write.
  581. *
  582. * @public
  583. * @category Error
  584. */
  585. class MongoClientBulkWriteError extends MongoServerError {
  586. /**
  587. * Initialize the client bulk write error.
  588. * @param message - The error message.
  589. */
  590. constructor(message) {
  591. super(message);
  592. this.writeConcernErrors = [];
  593. this.writeErrors = new Map();
  594. }
  595. get name() {
  596. return 'MongoClientBulkWriteError';
  597. }
  598. }
  599. exports.MongoClientBulkWriteError = MongoClientBulkWriteError;
  600. /**
  601. * An error indicating that an error occurred when processing bulk write results.
  602. *
  603. * @public
  604. * @category Error
  605. */
  606. class MongoClientBulkWriteCursorError extends MongoRuntimeError {
  607. /**
  608. * **Do not use this constructor!**
  609. *
  610. * Meant for internal use only.
  611. *
  612. * @remarks
  613. * This class is only meant to be constructed within the driver. This constructor is
  614. * not subject to semantic versioning compatibility guarantees and may change at any time.
  615. *
  616. * @public
  617. **/
  618. constructor(message) {
  619. super(message);
  620. }
  621. get name() {
  622. return 'MongoClientBulkWriteCursorError';
  623. }
  624. }
  625. exports.MongoClientBulkWriteCursorError = MongoClientBulkWriteCursorError;
  626. /**
  627. * An error indicating that an error occurred on the client when executing a client bulk write.
  628. *
  629. * @public
  630. * @category Error
  631. */
  632. class MongoClientBulkWriteExecutionError extends MongoRuntimeError {
  633. /**
  634. * **Do not use this constructor!**
  635. *
  636. * Meant for internal use only.
  637. *
  638. * @remarks
  639. * This class is only meant to be constructed within the driver. This constructor is
  640. * not subject to semantic versioning compatibility guarantees and may change at any time.
  641. *
  642. * @public
  643. **/
  644. constructor(message) {
  645. super(message);
  646. }
  647. get name() {
  648. return 'MongoClientBulkWriteExecutionError';
  649. }
  650. }
  651. exports.MongoClientBulkWriteExecutionError = MongoClientBulkWriteExecutionError;
  652. /**
  653. * An error generated when a ChangeStream operation fails to execute.
  654. *
  655. * @public
  656. * @category Error
  657. */
  658. class MongoChangeStreamError extends MongoRuntimeError {
  659. /**
  660. * **Do not use this constructor!**
  661. *
  662. * Meant for internal use only.
  663. *
  664. * @remarks
  665. * This class is only meant to be constructed within the driver. This constructor is
  666. * not subject to semantic versioning compatibility guarantees and may change at any time.
  667. *
  668. * @public
  669. **/
  670. constructor(message) {
  671. super(message);
  672. }
  673. get name() {
  674. return 'MongoChangeStreamError';
  675. }
  676. }
  677. exports.MongoChangeStreamError = MongoChangeStreamError;
  678. /**
  679. * An error thrown when the user calls a function or method not supported on a tailable cursor
  680. *
  681. * @public
  682. * @category Error
  683. */
  684. class MongoTailableCursorError extends MongoAPIError {
  685. /**
  686. * **Do not use this constructor!**
  687. *
  688. * Meant for internal use only.
  689. *
  690. * @remarks
  691. * This class is only meant to be constructed within the driver. This constructor is
  692. * not subject to semantic versioning compatibility guarantees and may change at any time.
  693. *
  694. * @public
  695. **/
  696. constructor(message = 'Tailable cursor does not support this operation') {
  697. super(message);
  698. }
  699. get name() {
  700. return 'MongoTailableCursorError';
  701. }
  702. }
  703. exports.MongoTailableCursorError = MongoTailableCursorError;
  704. /** An error generated when a GridFSStream operation fails to execute.
  705. *
  706. * @public
  707. * @category Error
  708. */
  709. class MongoGridFSStreamError extends MongoRuntimeError {
  710. /**
  711. * **Do not use this constructor!**
  712. *
  713. * Meant for internal use only.
  714. *
  715. * @remarks
  716. * This class is only meant to be constructed within the driver. This constructor is
  717. * not subject to semantic versioning compatibility guarantees and may change at any time.
  718. *
  719. * @public
  720. **/
  721. constructor(message) {
  722. super(message);
  723. }
  724. get name() {
  725. return 'MongoGridFSStreamError';
  726. }
  727. }
  728. exports.MongoGridFSStreamError = MongoGridFSStreamError;
  729. /**
  730. * An error generated when a malformed or invalid chunk is
  731. * encountered when reading from a GridFSStream.
  732. *
  733. * @public
  734. * @category Error
  735. */
  736. class MongoGridFSChunkError extends MongoRuntimeError {
  737. /**
  738. * **Do not use this constructor!**
  739. *
  740. * Meant for internal use only.
  741. *
  742. * @remarks
  743. * This class is only meant to be constructed within the driver. This constructor is
  744. * not subject to semantic versioning compatibility guarantees and may change at any time.
  745. *
  746. * @public
  747. **/
  748. constructor(message) {
  749. super(message);
  750. }
  751. get name() {
  752. return 'MongoGridFSChunkError';
  753. }
  754. }
  755. exports.MongoGridFSChunkError = MongoGridFSChunkError;
  756. /**
  757. * An error generated when a **parsable** unexpected response comes from the server.
  758. * This is generally an error where the driver in a state expecting a certain behavior to occur in
  759. * the next message from MongoDB but it receives something else.
  760. * This error **does not** represent an issue with wire message formatting.
  761. *
  762. * #### Example
  763. * When an operation fails, it is the driver's job to retry it. It must perform serverSelection
  764. * again to make sure that it attempts the operation against a server in a good state. If server
  765. * selection returns a server that does not support retryable operations, this error is used.
  766. * This scenario is unlikely as retryable support would also have been determined on the first attempt
  767. * but it is possible the state change could report a selectable server that does not support retries.
  768. *
  769. * @public
  770. * @category Error
  771. */
  772. class MongoUnexpectedServerResponseError extends MongoRuntimeError {
  773. /**
  774. * **Do not use this constructor!**
  775. *
  776. * Meant for internal use only.
  777. *
  778. * @remarks
  779. * This class is only meant to be constructed within the driver. This constructor is
  780. * not subject to semantic versioning compatibility guarantees and may change at any time.
  781. *
  782. * @public
  783. **/
  784. constructor(message, options) {
  785. super(message, options);
  786. }
  787. get name() {
  788. return 'MongoUnexpectedServerResponseError';
  789. }
  790. }
  791. exports.MongoUnexpectedServerResponseError = MongoUnexpectedServerResponseError;
  792. /**
  793. * @public
  794. * @category Error
  795. *
  796. * The `MongoOperationTimeoutError` class represents an error that occurs when an operation could not be completed within the specified `timeoutMS`.
  797. * It is generated by the driver in support of the "client side operation timeout" feature so inherits from `MongoDriverError`.
  798. * When `timeoutMS` is enabled `MongoServerError`s relating to `MaxTimeExpired` errors will be converted to `MongoOperationTimeoutError`
  799. *
  800. * @example
  801. * ```ts
  802. * try {
  803. * await blogs.insertOne(blogPost, { timeoutMS: 60_000 })
  804. * } catch (error) {
  805. * if (error instanceof MongoOperationTimeoutError) {
  806. * console.log(`Oh no! writer's block!`, error);
  807. * }
  808. * }
  809. * ```
  810. */
  811. class MongoOperationTimeoutError extends MongoDriverError {
  812. get name() {
  813. return 'MongoOperationTimeoutError';
  814. }
  815. }
  816. exports.MongoOperationTimeoutError = MongoOperationTimeoutError;
  817. /**
  818. * An error thrown when the user attempts to add options to a cursor that has already been
  819. * initialized
  820. *
  821. * @public
  822. * @category Error
  823. */
  824. class MongoCursorInUseError extends MongoAPIError {
  825. /**
  826. * **Do not use this constructor!**
  827. *
  828. * Meant for internal use only.
  829. *
  830. * @remarks
  831. * This class is only meant to be constructed within the driver. This constructor is
  832. * not subject to semantic versioning compatibility guarantees and may change at any time.
  833. *
  834. * @public
  835. **/
  836. constructor(message = 'Cursor is already initialized') {
  837. super(message);
  838. }
  839. get name() {
  840. return 'MongoCursorInUseError';
  841. }
  842. }
  843. exports.MongoCursorInUseError = MongoCursorInUseError;
  844. /**
  845. * An error generated when an attempt is made to operate
  846. * on a closed/closing server.
  847. *
  848. * @public
  849. * @category Error
  850. */
  851. class MongoServerClosedError extends MongoAPIError {
  852. /**
  853. * **Do not use this constructor!**
  854. *
  855. * Meant for internal use only.
  856. *
  857. * @remarks
  858. * This class is only meant to be constructed within the driver. This constructor is
  859. * not subject to semantic versioning compatibility guarantees and may change at any time.
  860. *
  861. * @public
  862. **/
  863. constructor(message = 'Server is closed') {
  864. super(message);
  865. }
  866. get name() {
  867. return 'MongoServerClosedError';
  868. }
  869. }
  870. exports.MongoServerClosedError = MongoServerClosedError;
  871. /**
  872. * An error thrown when an attempt is made to read from a cursor that has been exhausted
  873. *
  874. * @public
  875. * @category Error
  876. */
  877. class MongoCursorExhaustedError extends MongoAPIError {
  878. /**
  879. * **Do not use this constructor!**
  880. *
  881. * Meant for internal use only.
  882. *
  883. * @remarks
  884. * This class is only meant to be constructed within the driver. This constructor is
  885. * not subject to semantic versioning compatibility guarantees and may change at any time.
  886. *
  887. * @public
  888. **/
  889. constructor(message) {
  890. super(message || 'Cursor is exhausted');
  891. }
  892. get name() {
  893. return 'MongoCursorExhaustedError';
  894. }
  895. }
  896. exports.MongoCursorExhaustedError = MongoCursorExhaustedError;
  897. /**
  898. * An error generated when an attempt is made to operate on a
  899. * dropped, or otherwise unavailable, database.
  900. *
  901. * @public
  902. * @category Error
  903. */
  904. class MongoTopologyClosedError extends MongoAPIError {
  905. /**
  906. * **Do not use this constructor!**
  907. *
  908. * Meant for internal use only.
  909. *
  910. * @remarks
  911. * This class is only meant to be constructed within the driver. This constructor is
  912. * not subject to semantic versioning compatibility guarantees and may change at any time.
  913. *
  914. * @public
  915. **/
  916. constructor(message = 'Topology is closed') {
  917. super(message);
  918. }
  919. get name() {
  920. return 'MongoTopologyClosedError';
  921. }
  922. }
  923. exports.MongoTopologyClosedError = MongoTopologyClosedError;
  924. /**
  925. * An error generated when the MongoClient is closed and async
  926. * operations are interrupted.
  927. *
  928. * @public
  929. * @category Error
  930. */
  931. class MongoClientClosedError extends MongoAPIError {
  932. /**
  933. * **Do not use this constructor!**
  934. *
  935. * Meant for internal use only.
  936. *
  937. * @remarks
  938. * This class is only meant to be constructed within the driver. This constructor is
  939. * not subject to semantic versioning compatibility guarantees and may change at any time.
  940. *
  941. * @public
  942. **/
  943. constructor() {
  944. super('Operation interrupted because client was closed');
  945. }
  946. get name() {
  947. return 'MongoClientClosedError';
  948. }
  949. }
  950. exports.MongoClientClosedError = MongoClientClosedError;
  951. /**
  952. * An error indicating an issue with the network, including TCP errors and timeouts.
  953. * @public
  954. * @category Error
  955. */
  956. class MongoNetworkError extends MongoError {
  957. /**
  958. * **Do not use this constructor!**
  959. *
  960. * Meant for internal use only.
  961. *
  962. * @remarks
  963. * This class is only meant to be constructed within the driver. This constructor is
  964. * not subject to semantic versioning compatibility guarantees and may change at any time.
  965. *
  966. * @public
  967. **/
  968. constructor(message, options) {
  969. super(message, { cause: options?.cause });
  970. this.beforeHandshake = !!options?.beforeHandshake;
  971. }
  972. get name() {
  973. return 'MongoNetworkError';
  974. }
  975. }
  976. exports.MongoNetworkError = MongoNetworkError;
  977. /**
  978. * An error indicating a network timeout occurred
  979. * @public
  980. * @category Error
  981. *
  982. * @privateRemarks
  983. * mongodb-client-encryption has a dependency on this error with an instanceof check
  984. */
  985. class MongoNetworkTimeoutError extends MongoNetworkError {
  986. /**
  987. * **Do not use this constructor!**
  988. *
  989. * Meant for internal use only.
  990. *
  991. * @remarks
  992. * This class is only meant to be constructed within the driver. This constructor is
  993. * not subject to semantic versioning compatibility guarantees and may change at any time.
  994. *
  995. * @public
  996. **/
  997. constructor(message, options) {
  998. super(message, options);
  999. }
  1000. get name() {
  1001. return 'MongoNetworkTimeoutError';
  1002. }
  1003. }
  1004. exports.MongoNetworkTimeoutError = MongoNetworkTimeoutError;
  1005. /**
  1006. * An error used when attempting to parse a value (like a connection string)
  1007. * @public
  1008. * @category Error
  1009. */
  1010. class MongoParseError extends MongoDriverError {
  1011. /**
  1012. * **Do not use this constructor!**
  1013. *
  1014. * Meant for internal use only.
  1015. *
  1016. * @remarks
  1017. * This class is only meant to be constructed within the driver. This constructor is
  1018. * not subject to semantic versioning compatibility guarantees and may change at any time.
  1019. *
  1020. * @public
  1021. **/
  1022. constructor(message) {
  1023. super(message);
  1024. }
  1025. get name() {
  1026. return 'MongoParseError';
  1027. }
  1028. }
  1029. exports.MongoParseError = MongoParseError;
  1030. /**
  1031. * An error generated when the user supplies malformed or unexpected arguments
  1032. * or when a required argument or field is not provided.
  1033. *
  1034. *
  1035. * @public
  1036. * @category Error
  1037. */
  1038. class MongoInvalidArgumentError extends MongoAPIError {
  1039. /**
  1040. * **Do not use this constructor!**
  1041. *
  1042. * Meant for internal use only.
  1043. *
  1044. * @remarks
  1045. * This class is only meant to be constructed within the driver. This constructor is
  1046. * not subject to semantic versioning compatibility guarantees and may change at any time.
  1047. *
  1048. * @public
  1049. **/
  1050. constructor(message, options) {
  1051. super(message, options);
  1052. }
  1053. get name() {
  1054. return 'MongoInvalidArgumentError';
  1055. }
  1056. }
  1057. exports.MongoInvalidArgumentError = MongoInvalidArgumentError;
  1058. /**
  1059. * An error generated when a feature that is not enabled or allowed for the current server
  1060. * configuration is used
  1061. *
  1062. *
  1063. * @public
  1064. * @category Error
  1065. */
  1066. class MongoCompatibilityError extends MongoAPIError {
  1067. /**
  1068. * **Do not use this constructor!**
  1069. *
  1070. * Meant for internal use only.
  1071. *
  1072. * @remarks
  1073. * This class is only meant to be constructed within the driver. This constructor is
  1074. * not subject to semantic versioning compatibility guarantees and may change at any time.
  1075. *
  1076. * @public
  1077. **/
  1078. constructor(message) {
  1079. super(message);
  1080. }
  1081. get name() {
  1082. return 'MongoCompatibilityError';
  1083. }
  1084. }
  1085. exports.MongoCompatibilityError = MongoCompatibilityError;
  1086. /**
  1087. * An error generated when the user fails to provide authentication credentials before attempting
  1088. * to connect to a mongo server instance.
  1089. *
  1090. *
  1091. * @public
  1092. * @category Error
  1093. */
  1094. class MongoMissingCredentialsError extends MongoAPIError {
  1095. /**
  1096. * **Do not use this constructor!**
  1097. *
  1098. * Meant for internal use only.
  1099. *
  1100. * @remarks
  1101. * This class is only meant to be constructed within the driver. This constructor is
  1102. * not subject to semantic versioning compatibility guarantees and may change at any time.
  1103. *
  1104. * @public
  1105. **/
  1106. constructor(message) {
  1107. super(message);
  1108. }
  1109. get name() {
  1110. return 'MongoMissingCredentialsError';
  1111. }
  1112. }
  1113. exports.MongoMissingCredentialsError = MongoMissingCredentialsError;
  1114. /**
  1115. * An error generated when a required module or dependency is not present in the local environment
  1116. *
  1117. * @public
  1118. * @category Error
  1119. */
  1120. class MongoMissingDependencyError extends MongoAPIError {
  1121. /**
  1122. * **Do not use this constructor!**
  1123. *
  1124. * Meant for internal use only.
  1125. *
  1126. * @remarks
  1127. * This class is only meant to be constructed within the driver. This constructor is
  1128. * not subject to semantic versioning compatibility guarantees and may change at any time.
  1129. *
  1130. * @public
  1131. **/
  1132. constructor(message, options) {
  1133. super(message, options);
  1134. this.dependencyName = options.dependencyName;
  1135. }
  1136. get name() {
  1137. return 'MongoMissingDependencyError';
  1138. }
  1139. }
  1140. exports.MongoMissingDependencyError = MongoMissingDependencyError;
  1141. /**
  1142. * An error signifying a general system issue
  1143. * @public
  1144. * @category Error
  1145. */
  1146. class MongoSystemError extends MongoError {
  1147. /**
  1148. * **Do not use this constructor!**
  1149. *
  1150. * Meant for internal use only.
  1151. *
  1152. * @remarks
  1153. * This class is only meant to be constructed within the driver. This constructor is
  1154. * not subject to semantic versioning compatibility guarantees and may change at any time.
  1155. *
  1156. * @public
  1157. **/
  1158. constructor(message, reason) {
  1159. if (reason && reason.error) {
  1160. super(MongoError.buildErrorMessage(reason.error.message || reason.error), {
  1161. cause: reason.error
  1162. });
  1163. }
  1164. else {
  1165. super(message);
  1166. }
  1167. if (reason) {
  1168. this.reason = reason;
  1169. }
  1170. this.code = reason.error?.code;
  1171. }
  1172. get name() {
  1173. return 'MongoSystemError';
  1174. }
  1175. }
  1176. exports.MongoSystemError = MongoSystemError;
  1177. /**
  1178. * An error signifying a client-side server selection error
  1179. * @public
  1180. * @category Error
  1181. */
  1182. class MongoServerSelectionError extends MongoSystemError {
  1183. /**
  1184. * **Do not use this constructor!**
  1185. *
  1186. * Meant for internal use only.
  1187. *
  1188. * @remarks
  1189. * This class is only meant to be constructed within the driver. This constructor is
  1190. * not subject to semantic versioning compatibility guarantees and may change at any time.
  1191. *
  1192. * @public
  1193. **/
  1194. constructor(message, reason) {
  1195. super(message, reason);
  1196. }
  1197. get name() {
  1198. return 'MongoServerSelectionError';
  1199. }
  1200. }
  1201. exports.MongoServerSelectionError = MongoServerSelectionError;
  1202. /**
  1203. * An error thrown when the server reports a writeConcernError
  1204. * @public
  1205. * @category Error
  1206. */
  1207. class MongoWriteConcernError extends MongoServerError {
  1208. /**
  1209. * **Do not use this constructor!**
  1210. *
  1211. * Meant for internal use only.
  1212. *
  1213. * @remarks
  1214. * This class is only meant to be constructed within the driver. This constructor is
  1215. * not subject to semantic versioning compatibility guarantees and may change at any time.
  1216. *
  1217. * @public
  1218. **/
  1219. constructor(result) {
  1220. super({ ...result.writeConcernError, ...result });
  1221. this.errInfo = result.writeConcernError.errInfo;
  1222. this.result = result;
  1223. }
  1224. get name() {
  1225. return 'MongoWriteConcernError';
  1226. }
  1227. }
  1228. exports.MongoWriteConcernError = MongoWriteConcernError;
  1229. // https://github.com/mongodb/specifications/blob/master/source/retryable-reads/retryable-reads.md#retryable-error
  1230. const RETRYABLE_READ_ERROR_CODES = new Set([
  1231. exports.MONGODB_ERROR_CODES.HostUnreachable,
  1232. exports.MONGODB_ERROR_CODES.HostNotFound,
  1233. exports.MONGODB_ERROR_CODES.NetworkTimeout,
  1234. exports.MONGODB_ERROR_CODES.ShutdownInProgress,
  1235. exports.MONGODB_ERROR_CODES.PrimarySteppedDown,
  1236. exports.MONGODB_ERROR_CODES.SocketException,
  1237. exports.MONGODB_ERROR_CODES.NotWritablePrimary,
  1238. exports.MONGODB_ERROR_CODES.InterruptedAtShutdown,
  1239. exports.MONGODB_ERROR_CODES.InterruptedDueToReplStateChange,
  1240. exports.MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk,
  1241. exports.MONGODB_ERROR_CODES.NotPrimaryOrSecondary,
  1242. exports.MONGODB_ERROR_CODES.ExceededTimeLimit,
  1243. exports.MONGODB_ERROR_CODES.ReadConcernMajorityNotAvailableYet
  1244. ]);
  1245. // see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.md#terms
  1246. const RETRYABLE_WRITE_ERROR_CODES = RETRYABLE_READ_ERROR_CODES;
  1247. function needsRetryableWriteLabel(error, maxWireVersion, serverType) {
  1248. // pre-4.4 server, then the driver adds an error label for every valid case
  1249. // execute operation will only inspect the label, code/message logic is handled here
  1250. if (error instanceof MongoNetworkError) {
  1251. return true;
  1252. }
  1253. if (error instanceof MongoError) {
  1254. if ((maxWireVersion >= 9 || isRetryableWriteError(error)) &&
  1255. !error.hasErrorLabel(exports.MongoErrorLabel.HandshakeError)) {
  1256. // If we already have the error label no need to add it again. 4.4+ servers add the label.
  1257. // In the case where we have a handshake error, need to fall down to the logic checking
  1258. // the codes.
  1259. return false;
  1260. }
  1261. }
  1262. if (error instanceof MongoWriteConcernError) {
  1263. if (serverType === 'Mongos' && maxWireVersion < 9) {
  1264. // use original top-level code from server response
  1265. return RETRYABLE_WRITE_ERROR_CODES.has(error.result.code ?? 0);
  1266. }
  1267. const code = error.result.writeConcernError.code ?? Number(error.code);
  1268. return RETRYABLE_WRITE_ERROR_CODES.has(Number.isNaN(code) ? 0 : code);
  1269. }
  1270. if (error instanceof MongoError) {
  1271. return RETRYABLE_WRITE_ERROR_CODES.has(Number(error.code));
  1272. }
  1273. const isNotWritablePrimaryError = exports.LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE.test(error.message);
  1274. if (isNotWritablePrimaryError) {
  1275. return true;
  1276. }
  1277. const isNodeIsRecoveringError = exports.NODE_IS_RECOVERING_ERROR_MESSAGE.test(error.message);
  1278. if (isNodeIsRecoveringError) {
  1279. return true;
  1280. }
  1281. return false;
  1282. }
  1283. function isRetryableWriteError(error) {
  1284. return (error.hasErrorLabel(exports.MongoErrorLabel.RetryableWriteError) ||
  1285. error.hasErrorLabel(exports.MongoErrorLabel.PoolRequestedRetry));
  1286. }
  1287. /** Determines whether an error is something the driver should attempt to retry */
  1288. function isRetryableReadError(error) {
  1289. const hasRetryableErrorCode = typeof error.code === 'number' ? RETRYABLE_READ_ERROR_CODES.has(error.code) : false;
  1290. if (hasRetryableErrorCode) {
  1291. return true;
  1292. }
  1293. if (error instanceof MongoNetworkError) {
  1294. return true;
  1295. }
  1296. const isNotWritablePrimaryError = exports.LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE.test(error.message);
  1297. if (isNotWritablePrimaryError) {
  1298. return true;
  1299. }
  1300. const isNodeIsRecoveringError = exports.NODE_IS_RECOVERING_ERROR_MESSAGE.test(error.message);
  1301. if (isNodeIsRecoveringError) {
  1302. return true;
  1303. }
  1304. return false;
  1305. }
  1306. const SDAM_RECOVERING_CODES = new Set([
  1307. exports.MONGODB_ERROR_CODES.ShutdownInProgress,
  1308. exports.MONGODB_ERROR_CODES.PrimarySteppedDown,
  1309. exports.MONGODB_ERROR_CODES.InterruptedAtShutdown,
  1310. exports.MONGODB_ERROR_CODES.InterruptedDueToReplStateChange,
  1311. exports.MONGODB_ERROR_CODES.NotPrimaryOrSecondary
  1312. ]);
  1313. const SDAM_NOT_PRIMARY_CODES = new Set([
  1314. exports.MONGODB_ERROR_CODES.NotWritablePrimary,
  1315. exports.MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk,
  1316. exports.MONGODB_ERROR_CODES.LegacyNotPrimary
  1317. ]);
  1318. const SDAM_NODE_SHUTTING_DOWN_ERROR_CODES = new Set([
  1319. exports.MONGODB_ERROR_CODES.InterruptedAtShutdown,
  1320. exports.MONGODB_ERROR_CODES.ShutdownInProgress
  1321. ]);
  1322. function isRecoveringError(err) {
  1323. if (typeof err.code === 'number') {
  1324. // If any error code exists, we ignore the error.message
  1325. return SDAM_RECOVERING_CODES.has(err.code);
  1326. }
  1327. return (exports.LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE.test(err.message) ||
  1328. exports.NODE_IS_RECOVERING_ERROR_MESSAGE.test(err.message));
  1329. }
  1330. function isNotWritablePrimaryError(err) {
  1331. if (typeof err.code === 'number') {
  1332. // If any error code exists, we ignore the error.message
  1333. return SDAM_NOT_PRIMARY_CODES.has(err.code);
  1334. }
  1335. if (isRecoveringError(err)) {
  1336. return false;
  1337. }
  1338. return exports.LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE.test(err.message);
  1339. }
  1340. function isNodeShuttingDownError(err) {
  1341. return !!(typeof err.code === 'number' && SDAM_NODE_SHUTTING_DOWN_ERROR_CODES.has(err.code));
  1342. }
  1343. /**
  1344. * Determines whether SDAM can recover from a given error. If it cannot
  1345. * then the pool will be cleared, and server state will completely reset
  1346. * locally.
  1347. *
  1348. * @see https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md#not-writable-primary-and-node-is-recovering
  1349. */
  1350. function isSDAMUnrecoverableError(error) {
  1351. // NOTE: null check is here for a strictly pre-CMAP world, a timeout or
  1352. // close event are considered unrecoverable
  1353. if (error instanceof MongoParseError || error == null) {
  1354. return true;
  1355. }
  1356. return isRecoveringError(error) || isNotWritablePrimaryError(error);
  1357. }
  1358. function isNetworkTimeoutError(err) {
  1359. return !!(err instanceof MongoNetworkError && err.message.match(/timed out/));
  1360. }
  1361. function isResumableError(error, wireVersion) {
  1362. if (error == null || !(error instanceof MongoError)) {
  1363. return false;
  1364. }
  1365. if (error instanceof MongoNetworkError) {
  1366. return true;
  1367. }
  1368. if (error instanceof MongoServerSelectionError) {
  1369. return true;
  1370. }
  1371. if (wireVersion != null && wireVersion >= 9) {
  1372. // 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
  1373. if (error.code === exports.MONGODB_ERROR_CODES.CursorNotFound) {
  1374. return true;
  1375. }
  1376. return error.hasErrorLabel(exports.MongoErrorLabel.ResumableChangeStreamError);
  1377. }
  1378. if (typeof error.code === 'number') {
  1379. return exports.GET_MORE_RESUMABLE_CODES.has(error.code);
  1380. }
  1381. return false;
  1382. }
  1383. //# sourceMappingURL=error.js.map