bunway
Version:
Express-style routing toolkit built natively for Bun.
38 lines • 1.32 kB
TypeScript
import type { WayContext } from "./context";
/** Options bag for {@link HttpError}. */
export interface HttpErrorOptions {
cause?: unknown;
headers?: Record<string, string>;
body?: unknown;
}
/**
* Error helpers shared across bunWay.
*
* Example usage inside a route:
* ```ts
* app.get("/secret", () => {
* throw new HttpError(403, "Forbidden", {
* headers: { "X-Reason": "AUTH" },
* body: { error: "Forbidden" },
* });
* });
* ```
*
* The router (or errorHandler middleware) catches the error, calls
* {@link buildHttpErrorResponse}, and sends a properly typed response that
* respects the client's `Accept` header.
*/
export declare class HttpError extends Error {
readonly status: number;
readonly headers: Record<string, string>;
readonly body?: unknown;
constructor(status: number, message?: string, options?: HttpErrorOptions);
}
/** Type guard to detect HttpError instances coming from user code. */
export declare function isHttpError(value: unknown): value is HttpError;
/**
* Convert an {@link HttpError} into a Bun/Fetch {@link Response}.
* Automatically negotiates JSON vs text and ensures custom headers are applied.
*/
export declare function buildHttpErrorResponse(ctx: WayContext, error: HttpError): Response;
//# sourceMappingURL=errors.d.ts.map