@nuov.io/error-handler
v1.1.58
Published
The @nuov.io error handler library.
Readme
Error Handler
Description
Error Handler is a minimal TypeScript module that defines a common contract for handling errors. It exposes an ErrorHandler interface and an ErrorHandlerPrototype base class that delegates error handling to a constructor-injected arrow function. It is designed to be extended by concrete implementations (e.g., ThrowErrorHandler in @nuov.io/error-handling) and serves as the foundational building block for error handling across the @nuov.io ecosystem.
Installation
npm install @nuov.io/error-handlerUsage
Implementing the interface directly
import { ErrorHandler } from '@nuov.io/error-handler';
class ConsoleErrorHandler implements ErrorHandler {
public handleError(error: unknown): void {
console.error('An error occurred:', error);
}
}
const handler = new ConsoleErrorHandler();
handler.handleError(new Error('Something went wrong'));Using ErrorHandlerPrototype for delegation
ErrorHandlerPrototype lets you wrap any arrow function as an ErrorHandler, making it easy to create lightweight, inline handlers without writing a full class.
import { ErrorHandlerPrototype } from '@nuov.io/error-handler';
const handler = new ErrorHandlerPrototype((error) => {
console.error('Handled:', error);
});
handler.handleError(new Error('Something went wrong'));Extending ErrorHandlerPrototype
The most common pattern is to extend ErrorHandlerPrototype and pass a fixed arrow function to super():
import { ErrorHandlerPrototype } from '@nuov.io/error-handler';
class ThrowErrorHandler extends ErrorHandlerPrototype {
public constructor() {
super((error: Readonly<unknown>): void => {
throw error;
});
}
}
const handler = new ThrowErrorHandler();
handler.handleError(new Error('This will be rethrown'));API Reference
ErrorHandler (interface)
The contract that all error handlers must satisfy.
| Method | Returns | Description |
| ----------------------------- | ------- | ------------------ |
| handleError(error: unknown) | void | Handles the error. |
ErrorHandlerPrototype (class)
A concrete implementation of ErrorHandler that delegates to a constructor-injected arrow function.
Constructor
new ErrorHandlerPrototype(arrowFunction: (error: Readonly<unknown>) => void)| Parameter | Type | Description |
| --------------- | ------------------------------------ | ---------------------------------------------- |
| arrowFunction | (error: Readonly<unknown>) => void | The function invoked when an error is handled. |
Methods
| Method | Returns | Description |
| --------------------------------------- | ------- | ------------------------------------------------------------ |
| handleError(error: Readonly<unknown>) | void | Invokes the injected arrow function with the provided error. |
Features
- Minimal contract — a single-method
ErrorHandlerinterface that is easy to implement and mock - Delegation pattern —
ErrorHandlerPrototypewraps any arrow function, avoiding boilerplate for one-off handlers - Extensible — designed to be subclassed for concrete behaviors (e.g., throw, log, swallow)
- Zero dependencies — no runtime dependencies
- TypeScript-first — full type definitions included
- ESM-only — distributed as an ES module
License
MIT License
