Interface ErrorHandlerOptions

Catch-all error handler middleware.

Designed to be placed at the end of the global middleware chain to ensure any thrown errors are logged (optionally) and converted into standard HTTP responses. Works hand-in-hand with HttpError to provide precise status codes.

Example:

app.use(errorHandler({
logger: console.error,
map: (err) =>
err instanceof SyntaxError ? new HttpError(400, "Invalid JSON") : null,
}));

app.get("/secret", () => {
throw new HttpError(403, "Forbidden");
});
interface ErrorHandlerOptions {
    logger?: ((error, ctx) => void);
    map?: ((error, ctx) => undefined | null | HttpError | Error);
}

Properties

Properties

logger?: ((error, ctx) => void)

Optional logger invoked when an error bubbles up to the handler.

Type declaration

    • (error, ctx): void
    • Parameters

      Returns void

map?: ((error, ctx) => undefined | null | HttpError | Error)

Allows converting arbitrary errors into HttpError instances before they reach the catch-all branch. Return null/undefined to leave the original error untouched.

Type declaration