error.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import _ from "lodash";
  2. const errs = {
  3. PermissionError: function (_, previous) {
  4. Error.captureStackTrace(this, this.constructor);
  5. this.name = this.constructor.name;
  6. this.previous = previous;
  7. this.message = "Permission Denied";
  8. this.public = true;
  9. this.status = 403;
  10. },
  11. ItemNotFoundError: function (id, previous) {
  12. Error.captureStackTrace(this, this.constructor);
  13. this.name = this.constructor.name;
  14. this.previous = previous;
  15. this.message = "Not Found";
  16. if (id) {
  17. this.message = `Not Found - ${id}`;
  18. }
  19. this.public = true;
  20. this.status = 404;
  21. },
  22. AuthError: function (message, messageI18n, previous) {
  23. Error.captureStackTrace(this, this.constructor);
  24. this.name = this.constructor.name;
  25. this.previous = previous;
  26. this.message = message;
  27. this.message_i18n = messageI18n;
  28. this.public = true;
  29. this.status = 400;
  30. },
  31. InternalError: function (message, previous) {
  32. Error.captureStackTrace(this, this.constructor);
  33. this.name = this.constructor.name;
  34. this.previous = previous;
  35. this.message = message;
  36. this.status = 500;
  37. this.public = false;
  38. },
  39. InternalValidationError: function (message, previous) {
  40. Error.captureStackTrace(this, this.constructor);
  41. this.name = this.constructor.name;
  42. this.previous = previous;
  43. this.message = message;
  44. this.status = 400;
  45. this.public = false;
  46. },
  47. ConfigurationError: function (message, previous) {
  48. Error.captureStackTrace(this, this.constructor);
  49. this.name = this.constructor.name;
  50. this.previous = previous;
  51. this.message = message;
  52. this.status = 400;
  53. this.public = true;
  54. },
  55. CacheError: function (message, previous) {
  56. Error.captureStackTrace(this, this.constructor);
  57. this.name = this.constructor.name;
  58. this.message = message;
  59. this.previous = previous;
  60. this.status = 500;
  61. this.public = false;
  62. },
  63. ValidationError: function (message, previous) {
  64. Error.captureStackTrace(this, this.constructor);
  65. this.name = this.constructor.name;
  66. this.previous = previous;
  67. this.message = message;
  68. this.public = true;
  69. this.status = 400;
  70. },
  71. AssertionFailedError: function (message, previous) {
  72. Error.captureStackTrace(this, this.constructor);
  73. this.name = this.constructor.name;
  74. this.previous = previous;
  75. this.message = message;
  76. this.public = false;
  77. this.status = 400;
  78. },
  79. CommandError: function (stdErr, code, previous) {
  80. Error.captureStackTrace(this, this.constructor);
  81. this.name = this.constructor.name;
  82. this.previous = previous;
  83. this.message = stdErr;
  84. this.code = code;
  85. this.public = false;
  86. },
  87. };
  88. _.forEach(errs, (err) => {
  89. err.prototype = Object.create(Error.prototype);
  90. });
  91. export default errs;