ts-custom-error
Version:
Extend native Error to create custom errors
110 lines (91 loc) • 2.91 kB
JavaScript
function fixProto(target, prototype) {
var setPrototypeOf = Object.setPrototypeOf;
setPrototypeOf ? setPrototypeOf(target, prototype) : target.__proto__ = prototype;
}
function fixStack(target, fn) {
if (fn === void 0) {
fn = target.constructor;
}
var captureStackTrace = Error.captureStackTrace;
captureStackTrace && captureStackTrace(target, fn);
}
var __extends = undefined && undefined.__extends || function () {
var _extendStatics = function extendStatics(d, b) {
_extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function (d, b) {
d.__proto__ = b;
} || function (d, b) {
for (var p in b) {
if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
}
};
return _extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
_extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
}();
var CustomError = function (_super) {
__extends(CustomError, _super);
function CustomError(message, options) {
var _newTarget = this.constructor;
var _this = _super.call(this, message, options) || this;
Object.defineProperty(_this, 'name', {
value: _newTarget.name,
enumerable: false,
configurable: true
});
fixProto(_this, _newTarget.prototype);
fixStack(_this);
return _this;
}
return CustomError;
}(Error);
var __spreadArray = undefined && undefined.__spreadArray || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
function customErrorFactory(fn, parent) {
if (parent === void 0) {
parent = Error;
}
function CustomError() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (!(this instanceof CustomError)) return new (CustomError.bind.apply(CustomError, __spreadArray([void 0], args, false)))();
parent.apply(this, args);
Object.defineProperty(this, 'name', {
value: fn.name || parent.name,
enumerable: false,
configurable: true
});
fn.apply(this, args);
fixStack(this, CustomError);
}
return Object.defineProperties(CustomError, {
prototype: {
value: Object.create(parent.prototype, {
constructor: {
value: CustomError,
writable: true,
configurable: true
}
})
}
});
}
export { CustomError, customErrorFactory };
//# sourceMappingURL=custom-error.mjs.map