UNPKG

container.ts

Version:
155 lines 5.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var lodash_1 = require("lodash"); /** Error chain error names. */ var EErrorChainError; (function (EErrorChainError) { EErrorChainError["Serialise"] = "ErrorChainError.Serialise"; EErrorChainError["Deserialise"] = "ErrorChainError.Deserialise"; })(EErrorChainError = exports.EErrorChainError || (exports.EErrorChainError = {})); /** * ErrorChain class. * Serialisable, chained errors utility. */ var ErrorChain = /** @class */ (function () { function ErrorChain(data, cause) { /** Used for isErrorChain static method. */ this.isErrorChain = true; 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) { var instanceOf = error instanceof ErrorChain; var hasProperty = !!error.isErrorChain; return instanceOf || hasProperty; }; /** Returns true if error instance of Error or ErrorChain */ ErrorChain.isError = function (error) { return error instanceof Error || ErrorChain.isErrorChain(error); }; /** Returns name representation of error argument. */ ErrorChain.errorName = function (error) { if (ErrorChain.isErrorChain(error)) { return error.joinNames(); } else if (ErrorChain.isError(error) && error.name != null) { return error.name; } return String(error); }; /** * Construct error message from name, data and optional cause. * If data is a plain object it will be serialised into a JSON string. */ ErrorChain.messageConstructor = function (data, cause) { var message = data.name; if (data.value != null) { if (lodash_1.isPlainObject(data.value)) { message += " \"" + JSON.stringify(data.value) + "\""; } else { 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 || "") }; 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; 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: EErrorChainError.Deserialise, value: serialised }, error); } }; // TODO(L): Pretty print function for readability. ErrorChain.prototype.toString = function () { return this.message || ""; }; /** Join chained error names with a separator. */ 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 (ErrorChain.isError(this.cause)) { if (this.cause.name != null) { names.push(this.cause.name); } } } return names.join(separator); }; /** Return serialised data for this chained error. */ ErrorChain.prototype.serialise = function () { try { 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 (ErrorChain.isError(this.cause)) { chained.push(ErrorChain.serialiseError(this.cause)); } else if (lodash_1.isPlainObject(this.cause)) { chained.push(this.cause); } } return { ErrorChain: chained }; } catch (error) { throw new ErrorChain({ name: EErrorChainError.Serialise }, error); } }; return ErrorChain; }()); exports.ErrorChain = ErrorChain; //# sourceMappingURL=error-chain.js.map