custom-error.js 2.9 KB

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