Minimal Node.js process-level error handler utility for uncaught exceptions, unhandled rejections, warnings, and process exit events. Works in both CommonJS and ESM environments.
- Handles uncaught exceptions, unhandled rejections, warnings, and process exit events
- Pluggable logger (defaults to @purinton/log)
- Easy to add/remove handlers for testability
- Works in both CommonJS and ESM modules
npm install @purinton/errors
import { registerHandlers } from '@purinton/errors';
registerHandlers();
// Or with options:
// registerHandlers({ processObj: process, logger: customLogger });
// Simulate an uncaught exception
setTimeout(() => { throw new Error('Demo uncaught exception'); }, 1000);
const { registerHandlers } = require('@purinton/errors');
registerHandlers();
// Or with options:
// registerHandlers({ processObj: process, logger: customLogger });
// Simulate an uncaught exception
setTimeout(() => { throw new Error('Demo uncaught exception'); }, 1000);
Registers process-level exception handlers. Returns an object with a removeHandlers
function to detach all handlers (useful for testing).
options
(optional): An object with the following properties:processObj
(optional): The process object to attach handlers to (default:process
)logger
(optional): Logger for output (default: @purinton/log)
- Returns:
{ removeHandlers: () => void }
Type definitions are included:
export declare function registerHandlers(
options?: {
processObj?: NodeJS.Process,
logger?: typeof import('@purinton/log')
}
): { removeHandlers: () => void };
For help, questions, or to chat with the author and community, visit: