Skip to content

Commit 882b782

Browse files
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 #2995 --------- Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
1 parent 0fff2b0 commit 882b782

4 files changed

Lines changed: 18 additions & 5 deletions

File tree

packages/fresh/src/app.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { mergePath, type Method, UrlPatternRouter } from "./router.ts";
1212
import type { FreshConfig, ResolvedFreshConfig } from "./config.ts";
1313
import type { BuildCache } from "./build_cache.ts";
1414
import { HttpError } from "./error.ts";
15+
import { STATUS_TEXT } from "@std/http/status";
1516
import type { LayoutConfig, MaybeLazy, Route, RouteConfig } from "./types.ts";
1617
import type { RouteComponent } from "./segments.ts";
1718
import {
@@ -55,7 +56,8 @@ const DEFAULT_ERROR_HANDLER = async <State>(ctx: Context<State>) => {
5556
// deno-lint-ignore no-console
5657
console.error(error);
5758
}
58-
return new Response(error.message, { status: error.status });
59+
const message = error.message || STATUS_TEXT[error.status];
60+
return new Response(message, { status: error.status });
5961
}
6062

6163
// deno-lint-ignore no-console

packages/fresh/src/error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ErrorStatus, STATUS_TEXT } from "@std/http/status";
1+
import type { ErrorStatus } from "@std/http/status";
22

33
export type { ErrorStatus };
44

@@ -68,7 +68,7 @@ export class HttpError extends Error {
6868
*/
6969
constructor(
7070
status: ErrorStatus,
71-
message: string = STATUS_TEXT[status],
71+
message?: string,
7272
options?: ErrorOptions,
7373
) {
7474
super(message, options);

packages/fresh/src/error_test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ import { HttpError } from "./error.ts";
44
Deno.test("HttpError", () => {
55
const err = new HttpError(404);
66
expect(err.status).toEqual(404);
7-
expect(err.message).toEqual("Not Found");
87
expect(typeof err.stack).toEqual("string");
98

109
const err2 = new HttpError(500);
1110
expect(err2.status).toEqual(500);
12-
expect(err2.message).toEqual("Internal Server Error");
1311
expect(typeof err2.stack).toEqual("string");
1412
});
13+
14+
Deno.test("HttpError - message", () => {
15+
const err = new HttpError(500, "foo");
16+
expect(err.message).toEqual("foo");
17+
});
18+
19+
Deno.test("HttpError - cause", () => {
20+
const causeErr = new Error();
21+
const err = new HttpError(500, "foo", { cause: causeErr });
22+
expect(err.cause).toEqual(causeErr);
23+
});

packages/fresh/src/runtime/shared.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { ComponentChildren, VNode } from "preact";
22
import { BUILD_ID } from "@fresh/build-id";
33
import { assetInternal, assetSrcSetInternal } from "./shared_internal.ts";
44

5+
export { HttpError } from "../error.ts";
6+
57
/**
68
* Returns true when the current runtime is the browser and false otherwise. This is used for guard runtime-dependent code.
79
* Shorthand for the following:

0 commit comments

Comments
 (0)