error.js 38 KB

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