From aff31c0f15dbfe9732736d03654f2aa245a01d76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Tue, 27 Jun 2023 11:05:05 +0200 Subject: [PATCH] fix: Use custom 500 page for *all* handlers if present (#1362) --- src/server/context.ts | 15 ++++++++- tests/fixture_custom_500/deno.json | 13 ++++++++ tests/fixture_custom_500/dev.ts | 5 +++ tests/fixture_custom_500/fresh.gen.ts | 17 +++++++++++ tests/fixture_custom_500/main.ts | 10 ++++++ tests/fixture_custom_500/routes/_500.tsx | 5 +++ tests/fixture_custom_500/routes/index.tsx | 7 +++++ tests/main_test.ts | 37 +++++++++++++++++++++++ 8 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 tests/fixture_custom_500/deno.json create mode 100755 tests/fixture_custom_500/dev.ts create mode 100644 tests/fixture_custom_500/fresh.gen.ts create mode 100644 tests/fixture_custom_500/main.ts create mode 100644 tests/fixture_custom_500/routes/_500.tsx create mode 100644 tests/fixture_custom_500/routes/index.tsx diff --git a/src/server/context.ts b/src/server/context.ts index 372e2e1d..dd4e8acf 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -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: {}, diff --git a/tests/fixture_custom_500/deno.json b/tests/fixture_custom_500/deno.json new file mode 100644 index 00000000..fa257c4d --- /dev/null +++ b/tests/fixture_custom_500/deno.json @@ -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" + } +} diff --git a/tests/fixture_custom_500/dev.ts b/tests/fixture_custom_500/dev.ts new file mode 100755 index 00000000..2d85d6c1 --- /dev/null +++ b/tests/fixture_custom_500/dev.ts @@ -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"); diff --git a/tests/fixture_custom_500/fresh.gen.ts b/tests/fixture_custom_500/fresh.gen.ts new file mode 100644 index 00000000..b5f168f3 --- /dev/null +++ b/tests/fixture_custom_500/fresh.gen.ts @@ -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; diff --git a/tests/fixture_custom_500/main.ts b/tests/fixture_custom_500/main.ts new file mode 100644 index 00000000..dedce9cb --- /dev/null +++ b/tests/fixture_custom_500/main.ts @@ -0,0 +1,10 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; + +await start(manifest); diff --git a/tests/fixture_custom_500/routes/_500.tsx b/tests/fixture_custom_500/routes/_500.tsx new file mode 100644 index 00000000..06ec8219 --- /dev/null +++ b/tests/fixture_custom_500/routes/_500.tsx @@ -0,0 +1,5 @@ +import { ErrorPageProps } from "../../../server.ts"; + +export default function Error500Page({ error }: ErrorPageProps) { + return

Custom 500: {(error as Error).message}

; +} diff --git a/tests/fixture_custom_500/routes/index.tsx b/tests/fixture_custom_500/routes/index.tsx new file mode 100644 index 00000000..24b9a6c2 --- /dev/null +++ b/tests/fixture_custom_500/routes/index.tsx @@ -0,0 +1,7 @@ +import { Handlers } from "../../../server.ts"; + +export const handler: Handlers = { + GET(_req, ctx) { + throw new Error("Pickle Rick!"); + }, +}; diff --git a/tests/main_test.ts b/tests/main_test.ts index 73ed78af..d3441228 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -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"); +});