@piggly/fastify-chassis
Version:
An ESM/CommonJS toolkit to help you to do common operations in your back-end applications with Fastify and NodeJS.
37 lines • 1.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.processUncaught = void 0;
const tslib_1 = require("tslib");
/* eslint-disable no-console */
const ddd_toolkit_1 = require("@piggly/ddd-toolkit");
const event_bus_1 = tslib_1.__importDefault(require("@piggly/event-bus"));
/**
* Process uncaught error.
*
* It will wait for:
* - EventBus cleanup;
* - LoggerService flush and cleanup.
*
* You may not need to cleanup classes above when using this.
*
* @deprecated Use GracefulShutdownService instead. It will be removed in the next major version.
* @param {() => Promise<number>} beforeExit
* @param {number} exitCode Used when beforeExit is not provided. Default is 0.
* @returns The callback function.
* @since 5.1.0
* @author Caique Araujo <caique@piggly.com.br>
*/
const processUncaught = (beforeExit, exitCode = 0) => async (reason, origin) => {
console.error(reason, origin);
await event_bus_1.default.instance.cleanup();
const logger = ddd_toolkit_1.LoggerService.softResolve();
logger.error('UNCAUGHT_EXCEPTION_ERROR', reason, origin);
logger.flush();
await logger.cleanup();
if (beforeExit) {
process.exit(await beforeExit());
}
process.exit(exitCode);
};
exports.processUncaught = processUncaught;
//# sourceMappingURL=processUncaught.js.map