@nuov.io/error-handler
v1.1.63
Published
The @nuov.io error handler library.
Maintainers
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 concrete implementation that delegates error handling to a constructor-injected handler function. It is designed to be extended by other concrete implementations (e.g., ThrowErrorHandler in @nuov.io/error-handling) and serves as a foundational building block for error handling across the @nuov.io ecosystem.
Installation
npm install @nuov.io/error-handlerUsage
Implementing the ErrorHandler interface
import { type ErrorHandler } from "@nuov.io/error-handler";
class ConsoleErrorHandler implements ErrorHandler {
public handleError(error: unknown): void {
console.error("An error occurred:", error);
}
}
const errorHandler: ErrorHandler = new ConsoleErrorHandler();
errorHandler.handleError(new Error("Something went wrong"));Using the ErrorHandlerPrototype
import {
type ErrorHandler,
ErrorHandlerPrototype,
} from "@nuov.io/error-handler";
const errorHandler: ErrorHandler = new ErrorHandlerPrototype((error) => {
console.error("Handled:", error);
});
errorHandler.handleError(new Error("Something went wrong"));Extending the ErrorHandlerPrototype
import {
type ErrorHandler,
ErrorHandlerPrototype,
} from "@nuov.io/error-handler";
// taken from @nuov.io/error-handling
class ThrowErrorHandler extends ErrorHandlerPrototype {
public constructor() {
super((error: unknown): void => {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw error;
});
}
}
const errorHandler: ErrorHandler = new ThrowErrorHandler();
errorHandler.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 handler function.
Constructor
new ErrorHandlerPrototype(handlerFunction: (error: unknown) => void)| Parameter | Type | Description |
| ----------------- | -------------------------- | ------------------------------------------------------ |
| handlerFunction | (error: unknown) => void | The handler function invoked when an error is handled. |
Methods
| Method | Returns | Description |
| ----------------------------- | ------- | -------------------------------------------------------------- |
| handleError(error: unknown) | void | Invokes the injected handler function with the provided error. |
License
MIT License
