fix: use real 500 error page in dev mode (#2134)

Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
Christian Svensson
2023-12-04 12:06:06 +01:00
committed by GitHub
co-authored by Marvin Hagemeister
parent e78c6a90af
commit c4e09d1cef
7 changed files with 19 additions and 27 deletions
+1 -15
View File
@@ -226,21 +226,7 @@ export async function extractRoutes(
url, url,
name, name,
component, component,
handler: (req, ctx) => { handler: handler ?? router.defaultErrorHandler,
if (config.dev) {
const prevComp = error.component;
error.component = DefaultErrorHandler;
try {
return ctx.render();
} finally {
error.component = prevComp;
}
}
return handler
? handler(req, ctx)
: router.defaultErrorHandler(req, ctx, ctx.error);
},
csp: Boolean(routeConfig?.csp ?? false), csp: Boolean(routeConfig?.csp ?? false),
appWrapper: !routeConfig?.skipAppWrapper, appWrapper: !routeConfig?.skipAppWrapper,
inheritLayouts: !routeConfig?.skipInheritedLayouts, inheritLayouts: !routeConfig?.skipInheritedLayouts,
+5 -2
View File
@@ -359,9 +359,12 @@ export async function render<Data>(
pluginRenderResults: renderResults, pluginRenderResults: renderResults,
}); });
// Append error overlay // Dev only: Append error overlay
const devErrorUrl = withBase(DEV_ERROR_OVERLAY_URL, basePath); const devErrorUrl = withBase(DEV_ERROR_OVERLAY_URL, basePath);
if (error !== undefined && url.pathname !== devErrorUrl) { if (
error !== undefined && url.pathname !== devErrorUrl &&
opts.context.config.dev
) {
const url = new URL(devErrorUrl, "https://localhost/"); const url = new URL(devErrorUrl, "https://localhost/");
if (error instanceof Error) { if (error instanceof Error) {
let message = error.message; let message = error.message;
+2 -3
View File
@@ -82,10 +82,9 @@ export function defaultOtherHandler(_req: Request): Response {
export function defaultErrorHandler( export function defaultErrorHandler(
_req: Request, _req: Request,
_ctx: FreshContext, ctx: FreshContext,
err: unknown,
): Response { ): Response {
console.error(err); console.error(ctx.error);
return new Response(null, { return new Response(null, {
status: 500, status: 500,
+4 -6
View File
@@ -2,7 +2,6 @@ import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
import { STATUS_CODE } from "../server.ts"; import { STATUS_CODE } from "../server.ts";
import { import {
assertNotSelector, assertNotSelector,
assertSelector,
assertTextMany, assertTextMany,
assertTextMatch, assertTextMatch,
fetchHtml, fetchHtml,
@@ -118,17 +117,16 @@ Deno.test("show codeframe in dev mode even with custom 500", async () => {
await withFakeServe( await withFakeServe(
"./tests/fixture_dev_codeframe/dev.ts", "./tests/fixture_dev_codeframe/dev.ts",
async (server) => { async (server) => {
const doc = await server.getHtml(`/`); const { title } = await getErrorOverlay(server, "/");
assertSelector(doc, ".frsh-error-page"); assertEquals(title, "fail");
}, },
); );
await withFakeServe( await withFakeServe(
"./tests/fixture_dev_codeframe/main.ts", "./tests/fixture_dev_codeframe/main.ts",
async (server) => { async (server) => {
const doc = await server.getHtml(`/`); const doc = await server.getHtml("/");
assertNotSelector(doc, ".frsh-error-page"); assertNotSelector(doc, "#fresh-error-overlay");
assertSelector(doc, "h1");
}, },
); );
}); });
+2 -1
View File
@@ -7,7 +7,8 @@ Deno.test("error page rendered", async () => {
const resp = await server.get("/"); const resp = await server.get("/");
assertEquals(resp.status, STATUS_CODE.InternalServerError); assertEquals(resp.status, STATUS_CODE.InternalServerError);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8"); assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
await resp.text(); // Consume const body = await resp.text();
assertStringIncludes(body, "<p>500 page</p>");
const { title, stack } = await getErrorOverlay(server, "/"); const { title, stack } = await getErrorOverlay(server, "/");
assertStringIncludes(title, `boom!`); assertStringIncludes(title, `boom!`);
+2
View File
@@ -3,6 +3,7 @@
// This file is automatically updated during development when running `dev.ts`. // This file is automatically updated during development when running `dev.ts`.
import * as $_all_ from "./routes/[...all].ts"; import * as $_all_ from "./routes/[...all].ts";
import * as $_500 from "./routes/_500.tsx";
import * as $index from "./routes/index.tsx"; import * as $index from "./routes/index.tsx";
import { type Manifest } from "$fresh/server.ts"; import { type Manifest } from "$fresh/server.ts";
@@ -10,6 +11,7 @@ import { type Manifest } from "$fresh/server.ts";
const manifest = { const manifest = {
routes: { routes: {
"./routes/[...all].ts": $_all_, "./routes/[...all].ts": $_all_,
"./routes/_500.tsx": $_500,
"./routes/index.tsx": $index, "./routes/index.tsx": $index,
}, },
islands: {}, islands: {},
+3
View File
@@ -0,0 +1,3 @@
export default function Error500() {
return <p>500 page</p>;
}