feat: expose HttpError for clients (#3080)

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 <biwanczuk@gmail.com>
This commit is contained in:
Marvin Hagemeister
2026-03-26 09:10:21 +01:00
committed by GitHub
co-authored by Bartek Iwańczuk
parent 0fff2b04da
commit 882b7829fd
4 changed files with 18 additions and 5 deletions
+3 -1
View File
@@ -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 <State>(ctx: Context<State>) => {
// 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
+2 -2
View File
@@ -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);
+11 -2
View File
@@ -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);
});
+2
View File
@@ -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: