UNPKG

wkr-util

Version:
51 lines (40 loc) 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.attachLoggingOnUnhandledRejection = exports.attachLoggingOnUncaughtException = exports.fallbackOnUnhandledRejection = exports.fallbackOnUncaughtException = void 0; var _log = require("./log"); /* * Some useful functions for handling unhandledRejection and uncaughtException */ // a simple handler to use if no other is available const fallbackOnUncaughtException = err => { console.error('Uncaught exception. Exiting. Got error: ', err); process.exit(11); }; // a simple handler to use if no other is available exports.fallbackOnUncaughtException = fallbackOnUncaughtException; const fallbackOnUnhandledRejection = err => { console.error('Unhandled promise rejection. Exiting. Got error: ', err); process.exit(22); }; exports.fallbackOnUnhandledRejection = fallbackOnUnhandledRejection; const attachLoggingOnUncaughtException = logRepo => { // need to remove the fallback handler or else it will exit before our better handler is called process.off('uncaughtException', fallbackOnUncaughtException); process.on('uncaughtException', async err => { await logRepo.add('unhandled-exception', _log.logLevels.FATAL, 'Got an unhandled exception.', {}, undefined, err); console.error('Unhandled exception. Exiting. Got error:', err); process.exit(11); }); }; exports.attachLoggingOnUncaughtException = attachLoggingOnUncaughtException; const attachLoggingOnUnhandledRejection = logRepo => { // need to remove the fallback handler or else it will exit before our better handler is called process.off('unhandledRejection', fallbackOnUnhandledRejection); process.on('unhandledRejection', async err => { await logRepo.add('unhandled-promise-rejection', _log.logLevels.FATAL, 'Got an unhandled promise rejection.', {}, undefined, err); console.error('Unhandled promise rejection. Exiting. Got error:', err); process.exit(22); }); }; exports.attachLoggingOnUnhandledRejection = attachLoggingOnUnhandledRejection;