UNPKG

2.77 kBJavaScriptView Raw
1function fixProto(target, prototype) {
2 var setPrototypeOf = Object.setPrototypeOf;
3 setPrototypeOf ? setPrototypeOf(target, prototype) : target.__proto__ = prototype;
4}
5function fixStack(target, fn) {
6 if (fn === void 0) {
7 fn = target.constructor;
8 }
9
10 var captureStackTrace = Error.captureStackTrace;
11 captureStackTrace && captureStackTrace(target, fn);
12}
13
14var __extends = undefined && undefined.__extends || function () {
15 var extendStatics = function (d, b) {
16 extendStatics = Object.setPrototypeOf || {
17 __proto__: []
18 } instanceof Array && function (d, b) {
19 d.__proto__ = b;
20 } || function (d, b) {
21 for (var p in b) { if (b.hasOwnProperty(p)) { d[p] = b[p]; } }
22 };
23
24 return extendStatics(d, b);
25 };
26
27 return function (d, b) {
28 extendStatics(d, b);
29
30 function __() {
31 this.constructor = d;
32 }
33
34 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
35 };
36}();
37
38var CustomError = function (_super) {
39 __extends(CustomError, _super);
40
41 function CustomError(message) {
42 var _newTarget = this.constructor;
43
44 var _this = _super.call(this, message) || this;
45
46 Object.defineProperty(_this, 'name', {
47 value: _newTarget.name,
48 enumerable: false,
49 configurable: true
50 });
51 fixProto(_this, _newTarget.prototype);
52 fixStack(_this);
53 return _this;
54 }
55
56 return CustomError;
57}(Error);
58
59var __spreadArrays = undefined && undefined.__spreadArrays || function () {
60 var arguments$1 = arguments;
61
62 for (var s = 0, i = 0, il = arguments.length; i < il; i++) { s += arguments$1[i].length; }
63
64 for (var r = Array(s), k = 0, i = 0; i < il; i++) { for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) { r[k] = a[j]; } }
65
66 return r;
67};
68function customErrorFactory(fn, parent) {
69 if (parent === void 0) {
70 parent = Error;
71 }
72
73 function CustomError() {
74 var arguments$1 = arguments;
75
76 var args = [];
77
78 for (var _i = 0; _i < arguments.length; _i++) {
79 args[_i] = arguments$1[_i];
80 }
81
82 if (!(this instanceof CustomError)) { return new (CustomError.bind.apply(CustomError, __spreadArrays([void 0], args)))(); }
83 parent.apply(this, args);
84 Object.defineProperty(this, 'name', {
85 value: fn.name || parent.name,
86 enumerable: false,
87 configurable: true
88 });
89 fn.apply(this, args);
90 fixStack(this, CustomError);
91 }
92
93 return Object.defineProperties(CustomError, {
94 prototype: {
95 value: Object.create(parent.prototype, {
96 constructor: {
97 value: CustomError,
98 writable: true,
99 configurable: true
100 }
101 })
102 }
103 });
104}
105
106exports.CustomError = CustomError;
107exports.customErrorFactory = customErrorFactory;
108//# sourceMappingURL=custom-error.js.map