Source: errors.js

  1. /**
  2. * Append the old error message to the new one and keep its stack trace.
  3. * Example:
  4. * throw wrap(e, HighLevelError, "This error is more specific");
  5. */
  6. function wrap(
  7. oldError,
  8. newErrorType,
  9. ...args
  10. ) {
  11. const newError = new newErrorType(...args);
  12. let appendMsg;
  13. if (oldError instanceof Error) {
  14. appendMsg = oldError.message;
  15. newError.stack = oldError.stack;
  16. }
  17. else {
  18. appendMsg = oldError.toString();
  19. }
  20. newError.message += ":\n" + appendMsg;
  21. return newError;
  22. }
  23. /**
  24. * Ensures `args` contain an error message defaulting to `defaultMsg`.
  25. *
  26. * Modifies `args`.
  27. * @param {Array<str>} args The arguments to an Error object constructor.
  28. * @param {str} defaultMsg The error message to default to if there is none given.
  29. */
  30. function defaultMessage(args, defaultMsg) {
  31. args[0] = args[0] || defaultMsg;
  32. }
  33. /**
  34. * Base Error for when the bride can not handle the event.
  35. */
  36. class EventNotHandledError extends Error {
  37. constructor(...args) {
  38. defaultMessage(args, "The event could not be handled by the bridge");
  39. super(...args);
  40. this.name = "EventNotHandledError";
  41. this.reason = "m.event_not_handled";
  42. }
  43. }
  44. /**
  45. * The bridge decides that the event is too old to be sent.
  46. */
  47. class EventTooOldError extends EventNotHandledError {
  48. constructor(...args) {
  49. defaultMessage(args, "The event was too old to be handled by the bridge");
  50. super(...args);
  51. this.name = "EventTooOldError";
  52. this.reason = "m.event_too_old";
  53. }
  54. }
  55. /**
  56. * An unexpected internal error occured while the bridge handled the event.
  57. */
  58. class BridgeInternalError extends EventNotHandledError {
  59. constructor(...args) {
  60. defaultMessage(args, "The bridge experienced an internal error");
  61. super(...args);
  62. this.name = "EventTooOldError";
  63. this.reason = "m.internal_error";
  64. }
  65. }
  66. /**
  67. * The foreign network errored and the event couldn't be delivered.
  68. */
  69. class ForeignNetworkError extends EventNotHandledError {
  70. constructor(...args) {
  71. defaultMessage(args, "The foreign network experienced an error");
  72. super(...args);
  73. this.name = "ForeignNetworkError";
  74. this.reason = "m.foreign_network_error";
  75. }
  76. }
  77. /**
  78. * The event is not understood by the bridge.
  79. */
  80. class EventUnknownError extends EventNotHandledError {
  81. constructor(...args) {
  82. defaultMessage(args, "The event is not known to the bridge");
  83. super(...args);
  84. this.name = "EventUnknownError";
  85. this.reason = "m.event_unknown";
  86. }
  87. }
  88. module.exports = {
  89. wrap,
  90. EventNotHandledError,
  91. EventTooOldError,
  92. BridgeInternalError,
  93. ForeignNetworkError,
  94. EventUnknownError,
  95. }