Skip to content

Repository files navigation

eliware.org

@eliware/errors npm versionlicensebuild status

Minimal ESM-only Node.js process-level handler for uncaught exceptions, unhandled rejections, and warnings.


Table of Contents

Features

  • Handles uncaught exceptions, unhandled rejections, and warnings
  • Pluggable logger (defaults to @eliware/log)
  • Idempotent registration with safe, repeatable cleanup
  • Selectable events, one-shot handlers, and AbortSignal cleanup
  • Easy to add/remove handlers for testability
  • ESM-only package with TypeScript declarations

Installation

npm install @eliware/errors

Usage

ESM Example

import { registerHandlers } from '@eliware/errors';

registerHandlers();

// Or with options:
// registerHandlers({ processObj: process, log: 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):
    • processObj: Process-like event target (default: process)
    • log: Logger with error, warn, and debug methods (default: @eliware/log)
    • events: Supported event names to register (default: all three)
    • once: Use one-shot listeners when supported
    • signal: AbortSignal that automatically removes handlers
  • Returns: { removeHandlers: () => void }; call it to detach all three handlers.

Registration is idempotent per process-like object; repeated calls return the existing registration. Cleanup is safe to call repeatedly and removes the registration from the internal registry. Existing listeners are preserved. For production use, remember that handling uncaughtException can leave the process in an unsafe state; log it and shut down gracefully when appropriate.

TypeScript

Type definitions are included:

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

export default registerHandlers;

Support

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

Discordeliware.org

eliware.org on Discord

License

MIT © 2025 Eli Sterling, eliware.org

Links