wkr-util
Version:
Utility library for wkr project.
56 lines (46 loc) • 1.73 kB
JavaScript
/*
* Some useful functions for handling unhandledRejection and uncaughtException
*/
import {logLevels} from './log'
// a simple handler to use if no other is available
export const fallbackOnUncaughtException = err => {
console.error('Uncaught exception. Exiting. Got error: ', err)
process.exit(11)
}
// a simple handler to use if no other is available
export const fallbackOnUnhandledRejection = err => {
console.error('Unhandled promise rejection. Exiting. Got error: ', err)
process.exit(22)
}
export 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',
logLevels.FATAL,
'Got an unhandled exception.',
{},
undefined,
err,
)
console.error('Unhandled exception. Exiting. Got error:', err)
process.exit(11)
})
}
export 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',
logLevels.FATAL,
'Got an unhandled promise rejection.',
{},
undefined,
err,
)
console.error('Unhandled promise rejection. Exiting. Got error:', err)
process.exit(22)
})
}