UNPKG

actionhero

Version:

The reusable, scalable, and quick node.js API server for stateless and stateful applications

97 lines (96 loc) 3.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExceptionsInitializer = void 0; const index_1 = require("../index"); /** * Handlers for when things go wrong. */ class ExceptionsInitializer extends index_1.Initializer { constructor() { super(); this.report = (error, type, name, objects, severity) => { if (!severity) severity = "error"; for (const reporter of index_1.api.exceptionHandlers.reporters) { reporter(error, type, name, objects, severity); } }; this.initializer = (error, fullFilePath) => { const name = "initializer:" + fullFilePath; index_1.api.exceptionHandlers.report(error, "initializer", name, { fullFilePath: fullFilePath }, "alert"); }; this.action = (error, { to, action, params, duration, response, }) => { index_1.api.exceptionHandlers.report(error, "action", `action: ${action}`, { to, action, params, duration, error, response }, "alert"); }; this.task = (error, queue, task, workerId) => { let simpleName; try { simpleName = task["class"]; } catch (e) { simpleName = error.message; } const name = "task:" + simpleName; index_1.api.exceptionHandlers.report(error, "task", name, { task: task, queue: queue, workerId: workerId }, index_1.config.tasks.workerLogging.failure); }; this.name = "exceptions"; this.loadPriority = 1; } async initialize() { index_1.api.exceptionHandlers = { reporters: [], report: this.report, initializer: this.initializer, action: this.action, task: this.task, }; index_1.api.exceptionHandlers.reporters.push(consoleReporter); } } exports.ExceptionsInitializer = ExceptionsInitializer; const consoleReporter = (error, type, name, objects, severity) => { var _a, _b, _c; let message = ""; const data = (_a = error["data"]) !== null && _a !== void 0 ? _a : {}; if (type === "uncaught") { message = `Uncaught ${name}`; } else if (type === "action") { // no need to log anything, it was handled already by the actionProcessor } else if (type === "initializer") { message = `Error from Initializer`; } else if (type === "task") { message = `Error from Task`; data["name"] = name; data["queue"] = objects.queue; data["worker"] = objects.workerId; data["arguments"] = ((_b = objects === null || objects === void 0 ? void 0 : objects.task) === null || _b === void 0 ? void 0 : _b.args) ? JSON.stringify(objects.task.args[0]) : undefined; } else { message = `Error: ${(error === null || error === void 0 ? void 0 : error.message) || error.toString()}`; Object.getOwnPropertyNames(error) .filter((prop) => prop !== "message") .sort((a, b) => (a === "stack" || b === "stack" ? -1 : 1)) .forEach((prop) => (data[prop] = error[prop])); data["type"] = type; data["name"] = name; data["data"] = objects; } if (error["stack"]) { data["stack"] = error.stack; } else { data["stack"] = (_c = error.message) !== null && _c !== void 0 ? _c : error.toString(); } try { if (message) (0, index_1.log)(message, severity, data); } catch (e) { console.log(message, data); } };