custom-error.modern.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. function fixProto(target, prototype) {
  2. var setPrototypeOf = Object.setPrototypeOf;
  3. setPrototypeOf ? setPrototypeOf(target, prototype) : target.__proto__ = prototype;
  4. }
  5. function fixStack(target, fn) {
  6. if (fn === void 0) {
  7. fn = target.constructor;
  8. }
  9. var captureStackTrace = Error.captureStackTrace;
  10. captureStackTrace && captureStackTrace(target, fn);
  11. }
  12. var __extends = undefined && undefined.__extends || function () {
  13. var _extendStatics = function extendStatics(d, b) {
  14. _extendStatics = Object.setPrototypeOf || {
  15. __proto__: []
  16. } instanceof Array && function (d, b) {
  17. d.__proto__ = b;
  18. } || function (d, b) {
  19. for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
  20. };
  21. return _extendStatics(d, b);
  22. };
  23. return function (d, b) {
  24. if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  25. _extendStatics(d, b);
  26. function __() {
  27. this.constructor = d;
  28. }
  29. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  30. };
  31. }();
  32. var CustomError = function (_super) {
  33. __extends(CustomError, _super);
  34. function CustomError(message, options) {
  35. var _newTarget = this.constructor;
  36. var _this = _super.call(this, message, options) || this;
  37. Object.defineProperty(_this, 'name', {
  38. value: _newTarget.name,
  39. enumerable: false,
  40. configurable: true
  41. });
  42. fixProto(_this, _newTarget.prototype);
  43. fixStack(_this);
  44. return _this;
  45. }
  46. return CustomError;
  47. }(Error);
  48. var __spreadArray = undefined && undefined.__spreadArray || function (to, from, pack) {
  49. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  50. if (ar || !(i in from)) {
  51. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  52. ar[i] = from[i];
  53. }
  54. }
  55. return to.concat(ar || Array.prototype.slice.call(from));
  56. };
  57. function customErrorFactory(fn, parent) {
  58. if (parent === void 0) {
  59. parent = Error;
  60. }
  61. function CustomError() {
  62. var args = [];
  63. for (var _i = 0; _i < arguments.length; _i++) {
  64. args[_i] = arguments[_i];
  65. }
  66. if (!(this instanceof CustomError)) return new (CustomError.bind.apply(CustomError, __spreadArray([void 0], args, false)))();
  67. parent.apply(this, args);
  68. Object.defineProperty(this, 'name', {
  69. value: fn.name || parent.name,
  70. enumerable: false,
  71. configurable: true
  72. });
  73. fn.apply(this, args);
  74. fixStack(this, CustomError);
  75. }
  76. return Object.defineProperties(CustomError, {
  77. prototype: {
  78. value: Object.create(parent.prototype, {
  79. constructor: {
  80. value: CustomError,
  81. writable: true,
  82. configurable: true
  83. }
  84. })
  85. }
  86. });
  87. }
  88. export { CustomError, customErrorFactory };
  89. //# sourceMappingURL=custom-error.modern.mjs.map