From 882b7829fd52286bb31785eeb3b0cc7951552edc Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 26 Mar 2026 09:10:21 +0100 Subject: [PATCH] feat: expose `HttpError` for clients (#3080) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR exposes the `HttpError` class for clients. To do that I needed to make the error message optional to avoid bloating up the bundle with lots of status strings. Fixes https://github.com/denoland/fresh/issues/2995 --------- Co-authored-by: Bartek IwaƄczuk --- packages/fresh/src/app.ts | 4 +++- packages/fresh/src/error.ts | 4 ++-- packages/fresh/src/error_test.ts | 13 +++++++++++-- packages/fresh/src/runtime/shared.ts | 2 ++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/fresh/src/app.ts b/packages/fresh/src/app.ts index 0e592f8d..0c0e88bd 100644 --- a/packages/fresh/src/app.ts +++ b/packages/fresh/src/app.ts @@ -12,6 +12,7 @@ import { mergePath, type Method, UrlPatternRouter } from "./router.ts"; import type { FreshConfig, ResolvedFreshConfig } from "./config.ts"; import type { BuildCache } from "./build_cache.ts"; import { HttpError } from "./error.ts"; +import { STATUS_TEXT } from "@std/http/status"; import type { LayoutConfig, MaybeLazy, Route, RouteConfig } from "./types.ts"; import type { RouteComponent } from "./segments.ts"; import { @@ -55,7 +56,8 @@ const DEFAULT_ERROR_HANDLER = async (ctx: Context) => { // deno-lint-ignore no-console console.error(error); } - return new Response(error.message, { status: error.status }); + const message = error.message || STATUS_TEXT[error.status]; + return new Response(message, { status: error.status }); } // deno-lint-ignore no-console diff --git a/packages/fresh/src/error.ts b/packages/fresh/src/error.ts index 1851113f..a61a4c40 100644 --- a/packages/fresh/src/error.ts +++ b/packages/fresh/src/error.ts @@ -1,4 +1,4 @@ -import { type ErrorStatus, STATUS_TEXT } from "@std/http/status"; +import type { ErrorStatus } from "@std/http/status"; export type { ErrorStatus }; @@ -68,7 +68,7 @@ export class HttpError extends Error { */ constructor( status: ErrorStatus, - message: string = STATUS_TEXT[status], + message?: string, options?: ErrorOptions, ) { super(message, options); diff --git a/packages/fresh/src/error_test.ts b/packages/fresh/src/error_test.ts index 7038dc8a..c87e88f9 100644 --- a/packages/fresh/src/error_test.ts +++ b/packages/fresh/src/error_test.ts @@ -4,11 +4,20 @@ import { HttpError } from "./error.ts"; Deno.test("HttpError", () => { const err = new HttpError(404); expect(err.status).toEqual(404); - expect(err.message).toEqual("Not Found"); expect(typeof err.stack).toEqual("string"); const err2 = new HttpError(500); expect(err2.status).toEqual(500); - expect(err2.message).toEqual("Internal Server Error"); expect(typeof err2.stack).toEqual("string"); }); + +Deno.test("HttpError - message", () => { + const err = new HttpError(500, "foo"); + expect(err.message).toEqual("foo"); +}); + +Deno.test("HttpError - cause", () => { + const causeErr = new Error(); + const err = new HttpError(500, "foo", { cause: causeErr }); + expect(err.cause).toEqual(causeErr); +}); diff --git a/packages/fresh/src/runtime/shared.ts b/packages/fresh/src/runtime/shared.ts index c5bec2b4..6e05fed7 100644 --- a/packages/fresh/src/runtime/shared.ts +++ b/packages/fresh/src/runtime/shared.ts @@ -2,6 +2,8 @@ import type { ComponentChildren, VNode } from "preact"; import { BUILD_ID } from "@fresh/build-id"; import { assetInternal, assetSrcSetInternal } from "./shared_internal.ts"; +export { HttpError } from "../error.ts"; + /** * Returns true when the current runtime is the browser and false otherwise. This is used for guard runtime-dependent code. * Shorthand for the following: