Skip to content

purinton/errors

Repository files navigation

Purinton Dev

@purinton/errors npm versionlicensebuild status

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.


Table of Contents

Features

  • 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

Installation

npm install @purinton/errors

Usage

ESM Example

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);

CommonJS Example

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);

API

registerHandlers(options)

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 }

TypeScript

Type definitions are included:

export declare function registerHandlers(
  options?: {
    processObj?: NodeJS.Process,
    logger?: typeof import('@purinton/log')
  }
): { removeHandlers: () => void };

Support

For help, questions, or to chat with the author and community, visit:

DiscordPurinton Dev

Purinton Dev on Discord

License

MIT © 2025 Russell Purinton

Links