Skip to content
Merged
5 changes: 3 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { DEV_ERROR_OVERLAY_URL } from "./constants.ts";
import { BUILD_ID } from "./runtime/build_id.ts";
import { tracer } from "./otel.ts";
import type { HttpError } from "./error.ts";

export interface Island {
file: string | URL;
Expand All @@ -43,7 +44,7 @@ export interface FreshContext<State = unknown> {
*/
readonly url: URL;
readonly params: Record<string, string>;
readonly error: unknown;
readonly error: HttpError | null;
readonly info: Deno.ServeHandlerInfo;
/**
* Return a redirect response to the specified path. This is the
Expand Down Expand Up @@ -96,7 +97,7 @@ export class FreshReqContext<State>
params: Record<string, string>;
state: State = {} as State;
data: unknown = undefined;
error: unknown | null = null;
error: HttpError | null = null;
info: Deno.ServeHandlerInfo | Deno.ServeHandlerInfo;

next: FreshContext<State>["next"];
Expand Down
8 changes: 5 additions & 3 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { STATUS_TEXT } from "@std/http/status";
import { type ErrorStatus, STATUS_TEXT } from "@std/http/status";

export type { ErrorStatus };

/**
* Error that's thrown when a request fails. Correlates to a
Expand Down Expand Up @@ -54,7 +56,7 @@ export class HttpError extends Error {
* }
* ```
*/
status: number;
status: ErrorStatus;

/**
* Constructs a new instance.
Expand All @@ -65,7 +67,7 @@ export class HttpError extends Error {
* @param options Optional error options.
*/
constructor(
status: keyof typeof STATUS_TEXT,
status: ErrorStatus,
message: string = STATUS_TEXT[status],
options?: ErrorOptions,
) {
Expand Down
3 changes: 2 additions & 1 deletion src/middlewares/mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { FreshContext, FreshReqContext } from "../context.ts";
import type { App as _App } from "../app.ts";
import type { Define as _Define } from "../define.ts";
import type { HttpError } from "../error.ts";

/**
* A middleware function is the basic building block of Fresh. It allows you
Expand Down Expand Up @@ -98,7 +99,7 @@ export function runMiddlewares<State>(
try {
return await next(ctx);
} catch (err) {
ctx.error = err;
ctx.error = err as HttpError;
throw err;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/fs_routes/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ function errorMiddleware<State>(
try {
return await ctx.next();
} catch (err) {
(ctx as FreshReqContext<State>).error = err;
(ctx as FreshReqContext<State>).error = err as HttpError;
return mid(ctx);
}
};
Expand Down
Loading