Skip to content

Commit

Permalink
feat: add global error page
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-stumpf committed Dec 6, 2024
1 parent 13ff883 commit a503bce
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 27 deletions.
30 changes: 9 additions & 21 deletions src/app/error.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
"use client";

import { useEffect } from "react";
import { ErrorPage } from "@/components/ErrorPage";

const ErrorBoundary = ({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
error: Error & { digest?: string; statusCode?: string };
}) => {
useEffect(() => {
// Log the error to an error reporting service
console.error(error);
}, [error]);

console.log(error.cause);
return (
<div>
<h1>Something went wrong!</h1>
<button
type="button"
onClick={
// Attempt to recover by trying to re-render the segment
() => reset()
}
>
Try again
</button>
</div>
<ErrorPage
title="Something went wrong!"
correlationId={crypto.randomUUID()}
description={error.message}
errorCode={error.statusCode || "ONO"}
/>
);
};

Expand Down
13 changes: 7 additions & 6 deletions src/app/not-found.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Link from "next/link";
import "server-only";
import { ErrorPage } from "@/components/ErrorPage";

export default function NotFound() {
return (
<div>
<h1>Not Found</h1>
<p>Could not find requested resource</p>
<Link href="/">Return Home</Link>
</div>
<ErrorPage
errorCode="404"
title="Page not found"
description="We couldn't find the page you were looking for"
/>
);
}
61 changes: 61 additions & 0 deletions src/components/ErrorPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { DashboardContainer } from "@/components/DashboardContainer";
import { DataBlockWithCopy } from "@/components/requirements/DataBlockWithCopy";
import { Anchor } from "@/components/ui/Anchor";
import { buttonVariants } from "@/components/ui/Button";

// TODO: simple compound component when design is certain

const ErrorPage = ({
title,
description,
correlationId,
errorCode,
}: {
title: string;
description: string;
correlationId?: string;
errorCode: string;
}) => {
return (
<div className="relative h-screen">
<DashboardContainer className="flex size-full flex-col">
<main className="mx-auto flex h-full max-w-prose flex-col items-center justify-center text-center">
<h1 className="mb-4 text-pretty font-bold font-display text-4xl">
{title}
</h1>
<p className="mb-12 text-balance text-foreground-dimmed text-lg">
{description}
</p>
<Anchor
variant={"unstyled"}
href="/"
className={buttonVariants({
variant: "solid",
colorScheme: "primary",
})}
>
Return Home
</Anchor>
</main>
<footer className="mt-auto pb-8 text-center text-foreground-secondary text-xs sm:text-sm">
{correlationId && (
<code>
correlation id: <DataBlockWithCopy text={correlationId} />
</code>
)}
</footer>
</DashboardContainer>
<div className="-translate-y-1/2 -z-10 absolute top-1/2 w-full overflow-hidden text-center">
<div
className="font-black text-[clamp(128px,32vw,360px)] text-foreground leading-none tracking-tight opacity-20"
aria-hidden
>
{errorCode}
</div>
<div className="absolute inset-0 bg-gradient-to-t from-background" />
</div>
</div>
);
};

export { ErrorPage };

0 comments on commit a503bce

Please sign in to comment.