UNPKG

@angstone/monostone

Version:

monolitic event-sourced framework

110 lines 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ast = require("@angstone/node-util"); exports.MONOSTONE_ERROR = "MONOSTONE ERROR"; exports.OPERATIONAL_PREFIX = "OPERATIONAL ERROR: "; exports.FATAL_PREFIX = "FATAL ERROR: "; /** * Centralized unique error type */ class MonoError extends Error { /** * @param message string optional * @param details any details */ constructor(message, ...details) { super(message || "unknown error"); this.type = exports.MONOSTONE_ERROR; this.details = details; } } exports.MonoError = MonoError; /** * shortcuts functions to make easy dealing with errors */ exports.error = { /** * creates the error (same as new Error(...)) * @param message string optional * @param details any details */ is(message, ...details) { if (!details && !message) return new MonoError(); else if (!details && !!message) return new MonoError(message); else return new MonoError(message, details); }, /** * print error to console * @param message the text message of the error OR the generated error itself (Error or MonoError) * @param ...details any number of objects to include with the error */ op(message, ...details) { if (!message) { ast.warn(exports.OPERATIONAL_PREFIX + exports.error.is().message); } else if (typeof message == "string") { ast.warn(exports.OPERATIONAL_PREFIX + exports.error.is(message).message); } else { ast.warn(exports.OPERATIONAL_PREFIX + message.message); } if (details.length > 0) { details.forEach((detail) => { ast.warn(detail); }); } }, /** * print a fatal error to console and terminates the application * @param message the text message of the error OR the generated error itself (Error or MonoError) * @param ...details any number of objects to include with the error */ fatal(message, ...details) { if (!message) { ast.error(exports.FATAL_PREFIX + exports.error.is().message); } else if (typeof message == "string") { ast.error(exports.FATAL_PREFIX + exports.error.is(message).message); } else { ast.error(exports.FATAL_PREFIX + message.message); const internDetails = message.details; if (internDetails) { ast.error(internDetails); } } if (details.length > 0) { details.forEach((detail) => { ast.error(detail); }); } // It should terminate the application to avoid unpredicted states // try it gracefully process.exit(1); }, /** * throw a new error with the message * @param message can be a string or an instance of Error * @param ...details any number of objects to include with the error */ throw(message, ...details) { if (!details && !message) throw new MonoError(); else if (!details && !!message) { if (typeof message == "string") throw new MonoError(message); else throw message; } else { if (typeof message == "string") throw new MonoError(message, details); else throw message; } }, }; //# sourceMappingURL=error.js.map