feat: add nice error page during development (#122)
Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
co-authored by
Luca Casonato
parent
837d2a7f5e
commit
026ed86f40
@@ -4,6 +4,7 @@ export const ALIVE_URL = `${INTERNAL_PREFIX}/alive`;
|
|||||||
export const BUILD_ID = Deno.env.get("DENO_DEPLOYMENT_ID") ||
|
export const BUILD_ID = Deno.env.get("DENO_DEPLOYMENT_ID") ||
|
||||||
crypto.randomUUID();
|
crypto.randomUUID();
|
||||||
export const JS_PREFIX = `/js`;
|
export const JS_PREFIX = `/js`;
|
||||||
|
export const DEBUG = !Deno.env.get("DENO_DEPLOYMENT_ID");
|
||||||
|
|
||||||
export function bundleAssetUrl(path: string) {
|
export function bundleAssetUrl(path: string) {
|
||||||
return `${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}${path}`;
|
return `${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}${path}`;
|
||||||
|
|||||||
+31
-29
@@ -8,9 +8,14 @@ import {
|
|||||||
} from "./deps.ts";
|
} from "./deps.ts";
|
||||||
import { Manifest } from "./mod.ts";
|
import { Manifest } from "./mod.ts";
|
||||||
import { Bundler } from "./bundle.ts";
|
import { Bundler } from "./bundle.ts";
|
||||||
import { ALIVE_URL, INTERNAL_PREFIX, REFRESH_JS_URL } from "./constants.ts";
|
import {
|
||||||
import { JS_PREFIX } from "./constants.ts";
|
ALIVE_URL,
|
||||||
import { BUILD_ID } from "./constants.ts";
|
BUILD_ID,
|
||||||
|
INTERNAL_PREFIX,
|
||||||
|
JS_PREFIX,
|
||||||
|
REFRESH_JS_URL,
|
||||||
|
} from "./constants.ts";
|
||||||
|
import DefaultErrorHandler from "./default_error_page.tsx";
|
||||||
import {
|
import {
|
||||||
AppModule,
|
AppModule,
|
||||||
ErrorPage,
|
ErrorPage,
|
||||||
@@ -306,29 +311,19 @@ export class ServerContext {
|
|||||||
throw new Error("This page does not have a component to render.");
|
throw new Error("This page does not have a component to render.");
|
||||||
}
|
}
|
||||||
const preloads: string[] = [];
|
const preloads: string[] = [];
|
||||||
let resp;
|
const resp = internalRender({
|
||||||
try {
|
page,
|
||||||
resp = internalRender({
|
islands: this.#islands,
|
||||||
page,
|
app: this.#app,
|
||||||
islands: this.#islands,
|
imports,
|
||||||
app: this.#app,
|
preloads,
|
||||||
imports,
|
renderer: this.#renderer,
|
||||||
preloads,
|
url: new URL(req.url),
|
||||||
renderer: this.#renderer,
|
params,
|
||||||
url: new URL(req.url),
|
data,
|
||||||
params,
|
error,
|
||||||
data,
|
});
|
||||||
error,
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
console.error("Error rendering page", err);
|
|
||||||
return new Response("500 Internal Server Error", {
|
|
||||||
status: 500,
|
|
||||||
headers: {
|
|
||||||
"content-type": "text/plain",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
"content-type": "text/html; charset=utf-8",
|
"content-type": "text/html; charset=utf-8",
|
||||||
};
|
};
|
||||||
@@ -379,14 +374,20 @@ export class ServerContext {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const errorHandlerRender = genRender(this.#error, 500);
|
const errorHandlerRender = genRender(this.#error, 500);
|
||||||
const errorHandler = (req: Request, error: unknown) =>
|
const errorHandler = (req: Request, error: unknown) => {
|
||||||
this.#error.handler(
|
console.error(
|
||||||
|
"%cAn error occured during route handling or page rendering.",
|
||||||
|
"color:red",
|
||||||
|
error,
|
||||||
|
);
|
||||||
|
return this.#error.handler(
|
||||||
req,
|
req,
|
||||||
{
|
{
|
||||||
error,
|
error,
|
||||||
render: errorHandlerRender(req, {}, error),
|
render: errorHandlerRender(req, {}, error),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = this
|
routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = this
|
||||||
.#bundleAssetRoute();
|
.#bundleAssetRoute();
|
||||||
@@ -507,7 +508,8 @@ const DEFAULT_ERROR: ErrorPage = {
|
|||||||
route: "",
|
route: "",
|
||||||
url: "",
|
url: "",
|
||||||
name: "_500",
|
name: "_500",
|
||||||
handler: (req, ctx) => router.defaultErrorHandler(req, ctx.error),
|
component: DefaultErrorHandler,
|
||||||
|
handler: (_req, ctx) => ctx.render(),
|
||||||
csp: false,
|
csp: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
|
||||||
|
import { h } from "../runtime/deps.ts";
|
||||||
|
import { DEBUG } from "./constants.ts";
|
||||||
|
import type { ErrorPageProps } from "../runtime/types.ts";
|
||||||
|
|
||||||
|
export default function DefaultErrorPage(props: ErrorPageProps) {
|
||||||
|
const { error } = props;
|
||||||
|
|
||||||
|
let message = undefined;
|
||||||
|
if (DEBUG) {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
message = error.stack;
|
||||||
|
} else {
|
||||||
|
message = String(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "center",
|
||||||
|
alignItems: "center",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
border: "#f3f4f6 2px solid",
|
||||||
|
borderTop: "red 4px solid",
|
||||||
|
background: "#f9fafb",
|
||||||
|
margin: 16,
|
||||||
|
minWidth: "300px",
|
||||||
|
width: "50%",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<p
|
||||||
|
style={{
|
||||||
|
margin: 0,
|
||||||
|
fontSize: "12pt",
|
||||||
|
padding: 16,
|
||||||
|
fontFamily: "sans-serif",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
An error occured during route handling or page rendering.
|
||||||
|
</p>
|
||||||
|
{message && (
|
||||||
|
<pre
|
||||||
|
style={{
|
||||||
|
margin: 0,
|
||||||
|
fontSize: "12pt",
|
||||||
|
overflowY: "auto",
|
||||||
|
padding: 16,
|
||||||
|
paddingTop: 0,
|
||||||
|
fontFamily: "monospace",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{message}
|
||||||
|
</pre>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { ServerContext } from "../server.ts";
|
||||||
|
import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
|
||||||
|
import manifest from "./fixture_error/fresh.gen.ts";
|
||||||
|
|
||||||
|
const ctx = await ServerContext.fromManifest(manifest);
|
||||||
|
const router = ctx.handler();
|
||||||
|
|
||||||
|
Deno.test("error page rendered", async () => {
|
||||||
|
const resp = await router(new Request("https://fresh.deno.dev/"));
|
||||||
|
assert(resp);
|
||||||
|
assertEquals(resp.status, 500);
|
||||||
|
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
|
||||||
|
const body = await resp.text();
|
||||||
|
assertStringIncludes(
|
||||||
|
body,
|
||||||
|
`An error occured during route handling or page rendering.`,
|
||||||
|
);
|
||||||
|
assertStringIncludes(body, `Error: boom!`);
|
||||||
|
assertStringIncludes(body, `at render`);
|
||||||
|
});
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
# fresh project
|
|
||||||
|
|
||||||
### Usage
|
|
||||||
|
|
||||||
Start the project:
|
|
||||||
|
|
||||||
```
|
|
||||||
deno run -A --watch main.ts
|
|
||||||
```
|
|
||||||
|
|
||||||
After adding, removing, or moving a page in the `pages` directory, run:
|
|
||||||
|
|
||||||
```
|
|
||||||
fresh routes
|
|
||||||
```
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export * from "../../runtime.ts";
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
// DO NOT EDIT. This file is generated by `fresh`.
|
||||||
|
// This file SHOULD be checked into source version control.
|
||||||
|
// To update this file, run `fresh routes`.
|
||||||
|
|
||||||
|
import * as $0 from "./routes/index.tsx";
|
||||||
|
|
||||||
|
const manifest = {
|
||||||
|
routes: {
|
||||||
|
"./routes/index.tsx": $0,
|
||||||
|
},
|
||||||
|
islands: {},
|
||||||
|
baseUrl: import.meta.url,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default manifest;
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
/// <reference no-default-lib="true" />
|
||||||
|
/// <reference lib="dom" />
|
||||||
|
/// <reference lib="dom.asynciterable" />
|
||||||
|
/// <reference lib="deno.ns" />
|
||||||
|
/// <reference lib="deno.unstable" />
|
||||||
|
|
||||||
|
import { start } from "./server_deps.ts";
|
||||||
|
import manifest from "./fresh.gen.ts";
|
||||||
|
|
||||||
|
await start(manifest);
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
import { h } from "../client_deps.ts";
|
||||||
|
|
||||||
|
export default function Home(): null {
|
||||||
|
throw new Error("boom!");
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export * from "../../server.ts";
|
||||||
Reference in New Issue
Block a user