fix: Use custom 500 page for *all* handlers if present (#1362)
This commit is contained in:
+14
-1
@@ -440,7 +440,20 @@ export class ServerContext {
|
|||||||
const middlewareCtx: MiddlewareHandlerContext = {
|
const middlewareCtx: MiddlewareHandlerContext = {
|
||||||
next() {
|
next() {
|
||||||
const handler = handlers.shift()!;
|
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,
|
...connInfo,
|
||||||
state: {},
|
state: {},
|
||||||
|
|||||||
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
Executable
+5
@@ -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");
|
||||||
@@ -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;
|
||||||
@@ -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);
|
||||||
@@ -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!");
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -782,3 +782,40 @@ Deno.test("PORT environment variable", {
|
|||||||
await lines.cancel();
|
await lines.cancel();
|
||||||
serverProcess.kill("SIGTERM");
|
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");
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user