container.ts
Version:
Modular application framework
124 lines • 4.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var ErrorChain = /** @class */ (function () {
function ErrorChain(data, cause) {
var error = new Error(ErrorChain.messageConstructor(data, cause));
this.name = error.name = data.name;
this.stack = error.stack;
this.message = error.message;
this.value = data.value;
this.cause = cause;
}
/** Returns true if error instance of ErrorChain. */
ErrorChain.isErrorChain = function (error) {
return (error instanceof ErrorChain);
};
/** Returns true if error instance of Error or ErrorChain */
ErrorChain.isError = function (error) {
return (error instanceof Error) || (error instanceof ErrorChain);
};
/** Construct error message from data and optional cause. */
ErrorChain.messageConstructor = function (data, cause) {
var message = data.name;
if (data.value != null) {
message += " \"" + data.value + "\"";
}
if (cause != null) {
message += ": " + cause;
}
return message;
};
/** Return serialisable object of generic error data. */
ErrorChain.serialiseError = function (error) {
var serialised = {
name: String(error.name || ""),
stack: String(error.stack || ""),
message: String(error.stack || ""),
};
// Remove duplicate stack/message properties.
if (serialised.stack === serialised.message) {
delete serialised.message;
}
if (error.errno != null) {
serialised.errno = Number(error.errno);
}
if (error.code != null) {
serialised.code = String(error.code);
}
if (error.path != null) {
serialised.path = String(error.path);
}
if (error.syscall != null) {
serialised.syscall = String(error.syscall);
}
return serialised;
};
/** Return deserialised ErrorChain instance of serialised data. */
ErrorChain.deserialise = function (serialised) {
try {
var chained_1 = null;
serialised.ErrorChain.reduceRight(function (p, current) {
chained_1 = new ErrorChain(current, chained_1 || undefined);
chained_1 = Object.assign(chained_1, current);
return null;
}, null);
return chained_1;
}
catch (error) {
throw new ErrorChain({ name: ErrorChain.ERROR.DESERIALISE, value: serialised }, error);
}
};
ErrorChain.prototype.toString = function () { return this.message || ""; };
/** Join chained error names. */
ErrorChain.prototype.joinNames = function (separator) {
if (separator === void 0) { separator = "."; }
var names = [this.name];
if (this.cause != null) {
if (ErrorChain.isErrorChain(this.cause)) {
names.push(this.cause.joinNames(separator));
}
else if (this.cause instanceof Error) {
if (this.cause.name != null) {
names.push(this.cause.name);
}
}
}
return names.join(separator);
};
ErrorChain.prototype.serialise = function () {
try {
// Serialise this error object.
var chained = [{
name: this.name,
stack: String(this.stack || ""),
message: this.message,
}];
if (this.value != null) {
chained[0].value = this.value;
}
// Serialise chained error if available.
if (this.cause != null) {
if (ErrorChain.isErrorChain(this.cause)) {
var serialised = this.cause.serialise();
chained = chained.concat(serialised.ErrorChain);
}
else if (this.cause instanceof Error) {
chained.push(ErrorChain.serialiseError(this.cause));
}
}
return { ErrorChain: chained };
}
catch (error) {
throw new ErrorChain({ name: ErrorChain.ERROR.SERIALISE }, error);
}
};
/** Error names. */
ErrorChain.ERROR = {
SERIALISE: "SerialiseError",
DESERIALISE: "DeserialiseError",
};
return ErrorChain;
}());
exports.ErrorChain = ErrorChain;
exports.default = ErrorChain;
//# sourceMappingURL=ErrorChain.js.map