fix: Use custom 500 page for *all* handlers if present (#1362)

This commit is contained in:
Kamil Ogórek
2023-06-27 11:05:05 +02:00
committed by GitHub
parent ee3e4a1145
commit aff31c0f15
8 changed files with 108 additions and 1 deletions
+14 -1
View File
@@ -440,7 +440,20 @@ export class ServerContext {
const middlewareCtx: MiddlewareHandlerContext = {
next() {
const handler = handlers.shift()!;
return Promise.resolve(handler());
try {
// As the `handler` can be either sync or async, depending on the user's code,
// the current shape of our wrapper, that is `() => handler(req, middlewareCtx)`,
// doesn't guarantee that all possible errors will be captured.
// `Promise.resolve` accept the value that should be returned to the promise
// chain, however, if that value is produced by the external function call,
// the possible `Error`, will *not* be caught by any `.catch()` attached to that chain.
// Because of that, we need to make sure that the produced value is pushed
// through the pipeline only if function was called successfully, and handle
// the error case manually, by returning the `Error` as rejected promise.
return Promise.resolve(handler());
} catch (e) {
return Promise.reject(e);
}
},
...connInfo,
state: {},
+13
View File
@@ -0,0 +1,13 @@
{
"lock": false,
"imports": {
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
await dev(import.meta.url, "./main.ts");
+17
View File
@@ -0,0 +1,17 @@
// DO NOT EDIT. This file is generated by fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import * as $0 from "./routes/_500.tsx";
import * as $1 from "./routes/index.tsx";
const manifest = {
routes: {
"./routes/_500.tsx": $0,
"./routes/index.tsx": $1,
},
islands: {},
baseUrl: import.meta.url,
};
export default manifest;
+10
View File
@@ -0,0 +1,10 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
await start(manifest);
+5
View File
@@ -0,0 +1,5 @@
import { ErrorPageProps } from "../../../server.ts";
export default function Error500Page({ error }: ErrorPageProps) {
return <p class="custom-500">Custom 500: {(error as Error).message}</p>;
}
@@ -0,0 +1,7 @@
import { Handlers } from "../../../server.ts";
export const handler: Handlers = {
GET(_req, ctx) {
throw new Error("Pickle Rick!");
},
};
+37
View File
@@ -782,3 +782,40 @@ Deno.test("PORT environment variable", {
await lines.cancel();
serverProcess.kill("SIGTERM");
});
Deno.test("rendering custom _500.tsx page for default handlers", {
sanitizeOps: false,
sanitizeResources: false,
}, async (t) => {
// Preparation
const { serverProcess, lines, address } = await startFreshServer({
args: ["run", "-A", "./tests/fixture_custom_500/main.ts"],
});
await delay(100);
await t.step("SSR error is shown", async () => {
const resp = await fetch(address);
assertEquals(resp.status, Status.InternalServerError);
const text = await resp.text();
assertStringIncludes(text, "Custom 500: Pickle Rick!");
});
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
const page = await browser.newPage();
await page.goto(address, {
waitUntil: "networkidle2",
});
await t.step("error page is shown with error message", async () => {
const el = await page.waitForSelector(".custom-500");
const text = await page.evaluate((el) => el.textContent, el);
assertStringIncludes(text, "Custom 500: Pickle Rick!");
});
await browser.close();
await lines.cancel();
serverProcess.kill("SIGTERM");
});