Skip to content

Commit

Permalink
Improve error handling and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
pietvanzoen committed Jul 9, 2023
1 parent ae04825 commit cec03b8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
10 changes: 9 additions & 1 deletion app/components/Head.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Links, Meta } from "@remix-run/react";
import type { LinkHTMLAttributes } from "react";
import { makeTitle } from "~/utils/meta";
import type { Theme } from "./ThemeStyles";
import ThemeStyles from "./ThemeStyles";

Expand Down Expand Up @@ -108,7 +109,13 @@ const LINKS = [
},
];

export default function Head({ theme }: { theme: Theme }) {
export default function Head({
theme,
title,
}: {
theme: Theme;
title?: string;
}) {
return (
<head>
<meta charSet="utf-8" />
Expand All @@ -120,6 +127,7 @@ export default function Head({ theme }: { theme: Theme }) {
<link key={i} {...link} />
))}
<Meta />
{title && <title>{makeTitle([title])}</title>}
<Links />
<ThemeStyles theme={theme || "system"} />
</head>
Expand Down
5 changes: 4 additions & 1 deletion app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { Response } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import isbot from "isbot";
import { renderToPipeableStream } from "react-dom/server";
import debug from "debug";
const log = debug("app:entry");

const ABORT_DELAY = 5_000;

Expand Down Expand Up @@ -105,10 +107,11 @@ function handleBrowserRequest(
pipe(body);
},
onShellError(error: unknown) {
log("shell error", error);
reject(error);
},
onError(error: unknown) {
console.error(error);
log(error);
responseStatusCode = 500;
},
}
Expand Down
55 changes: 29 additions & 26 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import type { Theme } from "./components/ThemeStyles";
import Head from "./components/Head";
import NavigationLoader from "./components/NavigationLoader";
import Header from "./components/Header";
import debug from "debug";
const logError = debug("app:rootError");

export const links: LinksFunction = () => {
return [{ rel: "stylesheet", href: stylesUrl }];
Expand All @@ -30,28 +32,36 @@ export async function loader({ request }: LoaderArgs) {
});
}

function Document({ children }: { children: React.ReactNode }) {
function Document({
title,
children,
}: {
children: React.ReactNode;
title?: string;
}) {
const data = useLoaderData<typeof loader>();
return (
<html lang="en">
<Head theme={data?.user?.theme as Theme} />
<Head theme={data?.user?.theme as Theme} title={title} />
<body>
<Header showNav={Boolean(data?.user)} />

<main id="main">{children}</main>

<footer>
<small>
<p>
Version:{" "}
<a
href={`https://github.com/pietvanzoen/gifable/tree/${
data?.buildSHA === "dev" ? "main" : data?.buildSHA
}`}
>
{data?.buildSHA}
</a>
</p>
{data?.buildSHA && (
<p>
Version:{" "}
<a
href={`https://github.com/pietvanzoen/gifable/tree/${
data?.buildSHA === "dev" ? "main" : data?.buildSHA
}`}
>
{data?.buildSHA}
</a>
</p>
)}

<div>
<a
Expand Down Expand Up @@ -94,6 +104,7 @@ export default function App() {

export function ErrorBoundary() {
let error = useRouteError();
logError(error);
if (isRouteErrorResponse(error)) {
return (
<Document title={`${error.status} ${error.statusText}`}>
Expand All @@ -105,22 +116,14 @@ export function ErrorBoundary() {
</div>
</Document>
);
} else if (error instanceof Error) {
return (
<Document title="Uh-oh!">
<div className="notice">
<h1>Error</h1>
<p>{error.message}</p>
<p>The stack trace is:</p>
<pre>{error.stack}</pre>
</div>
</Document>
);
} else {
return (
<Document title="Uh-oh!">
<h1>Unknown Error</h1>
<p>Something went wrong.</p>
<Document title="Something went wrong">
<center>
<h1>🙈 Whoops, something went wrong</h1>
<img src="/images/500.jpg" alt="500 error" width="300" />
<p>Sorry about that. Please try again.</p>
</center>
</Document>
);
}
Expand Down
4 changes: 0 additions & 4 deletions app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,3 @@ export default function MediaRoute() {
</>
);
}

export function ErrorBoundary() {
return <div className="notice">I did a whoopsies.</div>;
}
Binary file added public/images/500.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cec03b8

Please sign in to comment.