feat: support custom error pages (#86)

This commit is contained in:
Leo Kettmeir
2022-02-15 01:17:47 +01:00
committed by GitHub
parent 64fbcabf67
commit d334b53320
9 changed files with 243 additions and 26 deletions
+7
View File
@@ -0,0 +1,7 @@
/** @jsx h */
import { h, UnknownPageProps } from "../deps.ts";
export default function NotFoundPage({ url }: UnknownPageProps) {
return <p>404 not found: {url.pathname}</p>;
}
+7
View File
@@ -0,0 +1,7 @@
/** @jsx h */
import { ErrorPageProps, h } from "../deps.ts";
export default function Error500Page({ error }: ErrorPageProps) {
return <p>500 internal error: {(error as Error).message}</p>;
}
+7
View File
@@ -0,0 +1,7 @@
import { HandlerContext } from "../../../server.ts";
export const handler = {
GET(_ctx: HandlerContext) {
throw Error("it errored!");
},
};
+16 -10
View File
@@ -6,11 +6,14 @@ import * as $0 from "./pages/index.tsx";
import * as $1 from "./pages/static.tsx";
import * as $2 from "./pages/intercept_args.tsx";
import * as $3 from "./pages/props/[id].tsx";
import * as $4 from "./pages/_middleware.ts";
import * as $5 from "./pages/books/[id].tsx";
import * as $6 from "./pages/api/get_only.ts";
import * as $7 from "./pages/intercept.tsx";
import * as $8 from "./pages/[name].tsx";
import * as $4 from "./pages/failure.ts";
import * as $5 from "./pages/_middleware.ts";
import * as $6 from "./pages/books/[id].tsx";
import * as $7 from "./pages/api/get_only.ts";
import * as $8 from "./pages/_500.tsx";
import * as $9 from "./pages/intercept.tsx";
import * as $10 from "./pages/_404.tsx";
import * as $11 from "./pages/[name].tsx";
const routes = {
pages: {
@@ -18,11 +21,14 @@ const routes = {
"./pages/static.tsx": $1,
"./pages/intercept_args.tsx": $2,
"./pages/props/[id].tsx": $3,
"./pages/_middleware.ts": $4,
"./pages/books/[id].tsx": $5,
"./pages/api/get_only.ts": $6,
"./pages/intercept.tsx": $7,
"./pages/[name].tsx": $8,
"./pages/failure.ts": $4,
"./pages/_middleware.ts": $5,
"./pages/books/[id].tsx": $6,
"./pages/api/get_only.ts": $7,
"./pages/_500.tsx": $8,
"./pages/intercept.tsx": $9,
"./pages/_404.tsx": $10,
"./pages/[name].tsx": $11,
},
baseUrl: import.meta.url,
};
+10
View File
@@ -123,6 +123,8 @@ Deno.test("/api/xyz not found", async () => {
const resp = await router(new Request("https://fresh.deno.dev/api/xyz"));
assert(resp);
assertEquals(resp.status, 404);
const body = await resp.text();
assert(body.includes("404 not found: /api/xyz"));
});
Deno.test("/static page prerender", async () => {
@@ -160,3 +162,11 @@ Deno.test("redirect /pages/fresh/ to /pages/fresh", async () => {
"https://fresh.deno.dev/pages/fresh",
);
});
Deno.test("/failure", async () => {
const resp = await router(new Request("https://fresh.deno.dev/failure"));
assert(resp);
assertEquals(resp.status, 500);
const body = await resp.text();
assert(body.includes("500 internal error: it errored!"));
});