Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

error.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. 'use strict';
  2. const mongoErrorContextSymbol = Symbol('mongoErrorContextSymbol');
  3. /**
  4. * Creates a new MongoError
  5. *
  6. * @augments Error
  7. * @param {Error|string|object} message The error message
  8. * @property {string} message The error message
  9. * @property {string} stack The error call stack
  10. */
  11. class MongoError extends Error {
  12. constructor(message) {
  13. if (message instanceof Error) {
  14. super(message.message);
  15. this.stack = message.stack;
  16. } else {
  17. if (typeof message === 'string') {
  18. super(message);
  19. } else {
  20. super(message.message || message.errmsg || message.$err || 'n/a');
  21. for (var name in message) {
  22. this[name] = message[name];
  23. }
  24. }
  25. Error.captureStackTrace(this, this.constructor);
  26. }
  27. this.name = 'MongoError';
  28. this[mongoErrorContextSymbol] = this[mongoErrorContextSymbol] || {};
  29. }
  30. /**
  31. * Creates a new MongoError object
  32. *
  33. * @param {Error|string|object} options The options used to create the error.
  34. * @return {MongoError} A MongoError instance
  35. * @deprecated Use `new MongoError()` instead.
  36. */
  37. static create(options) {
  38. return new MongoError(options);
  39. }
  40. }
  41. /**
  42. * Creates a new MongoNetworkError
  43. *
  44. * @param {Error|string|object} message The error message
  45. * @property {string} message The error message
  46. * @property {string} stack The error call stack
  47. */
  48. class MongoNetworkError extends MongoError {
  49. constructor(message) {
  50. super(message);
  51. this.name = 'MongoNetworkError';
  52. // This is added as part of the transactions specification
  53. this.errorLabels = ['TransientTransactionError'];
  54. }
  55. }
  56. /**
  57. * An error used when attempting to parse a value (like a connection string)
  58. *
  59. * @param {Error|string|object} message The error message
  60. * @property {string} message The error message
  61. */
  62. class MongoParseError extends MongoError {
  63. constructor(message) {
  64. super(message);
  65. this.name = 'MongoParseError';
  66. }
  67. }
  68. /**
  69. * An error signifying a timeout event
  70. *
  71. * @param {Error|string|object} message The error message
  72. * @property {string} message The error message
  73. */
  74. class MongoTimeoutError extends MongoError {
  75. constructor(message) {
  76. super(message);
  77. this.name = 'MongoTimeoutError';
  78. }
  79. }
  80. /**
  81. * An error thrown when the server reports a writeConcernError
  82. *
  83. * @param {Error|string|object} message The error message
  84. * @param {object} result The result document (provided if ok: 1)
  85. * @property {string} message The error message
  86. * @property {object} [result] The result document (provided if ok: 1)
  87. */
  88. class MongoWriteConcernError extends MongoError {
  89. constructor(message, result) {
  90. super(message);
  91. this.name = 'MongoWriteConcernError';
  92. if (result != null) {
  93. this.result = result;
  94. }
  95. }
  96. }
  97. // see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#terms
  98. const RETRYABLE_ERROR_CODES = new Set([
  99. 6, // HostUnreachable
  100. 7, // HostNotFound
  101. 89, // NetworkTimeout
  102. 91, // ShutdownInProgress
  103. 189, // PrimarySteppedDown
  104. 9001, // SocketException
  105. 10107, // NotMaster
  106. 11600, // InterruptedAtShutdown
  107. 11602, // InterruptedDueToReplStateChange
  108. 13435, // NotMasterNoSlaveOk
  109. 13436 // NotMasterOrSecondary
  110. ]);
  111. /**
  112. * Determines whether an error is something the driver should attempt to retry
  113. *
  114. * @param {MongoError|Error} error
  115. */
  116. function isRetryableError(error) {
  117. return (
  118. RETRYABLE_ERROR_CODES.has(error.code) ||
  119. error instanceof MongoNetworkError ||
  120. error.message.match(/not master/) ||
  121. error.message.match(/node is recovering/)
  122. );
  123. }
  124. module.exports = {
  125. MongoError,
  126. MongoNetworkError,
  127. MongoParseError,
  128. MongoTimeoutError,
  129. MongoWriteConcernError,
  130. mongoErrorContextSymbol,
  131. isRetryableError
  132. };