bunway
Version:
Express-style routing toolkit built natively for Bun.
36 lines • 1.38 kB
TypeScript
import { HttpError } from "../core/errors";
import type { WayContext } from "../core/context";
import type { Handler } from "../core/router";
/**
* 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 {@link HttpError} to provide precise status codes.
*
* Example:
* ```ts
* 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");
* });
* ```
*/
export interface ErrorHandlerOptions {
/** Optional logger invoked when an error bubbles up to the handler. */
logger?: (error: unknown, ctx: WayContext) => void;
/**
* Allows converting arbitrary errors into {@link HttpError} instances before
* they reach the catch-all branch. Return `null`/`undefined` to leave the
* original error untouched.
*/
map?: (error: unknown, ctx: WayContext) => HttpError | Error | null | undefined;
}
/** Create an error-handling middleware with optional logging and mapping. */
export declare function errorHandler(options?: ErrorHandlerOptions): Handler;
//# sourceMappingURL=errorHandler.d.ts.map