@malagu/core
Version:
47 lines • 1.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApplicationError = void 0;
var ApplicationError;
(function (ApplicationError) {
const codes = [];
function declare(code, factory) {
if (codes.indexOf(code) !== -1) {
throw new Error(`An application error for '${code}' code is already declared`);
}
const constructorOpt = Object.assign((...args) => new Impl(code, factory(...args), constructorOpt), {
code,
is(arg) {
return arg instanceof Impl && arg.code === code;
}
});
return constructorOpt;
}
ApplicationError.declare = declare;
function is(arg) {
return arg instanceof Impl;
}
ApplicationError.is = is;
function fromJson(code, raw) {
return new Impl(code, raw);
}
ApplicationError.fromJson = fromJson;
class Impl extends Error {
constructor(code, raw, constructorOpt) {
super(raw.message);
this.code = code;
this.data = raw.data;
Object.setPrototypeOf(this, Impl.prototype);
if (raw.stack) {
this.stack = raw.stack;
}
else if (Error.captureStackTrace && constructorOpt) {
Error.captureStackTrace(this, constructorOpt);
}
}
toJson() {
const { message, data, stack } = this;
return { message, data, stack };
}
}
})(ApplicationError = exports.ApplicationError || (exports.ApplicationError = {}));
//# sourceMappingURL=application-error.js.map