Add root ErrorBoundary to both apps, simplify withDb

Both apps now have proper ErrorBoundary exports in root.tsx:
- 404: "Page not found"
- 503: "Service temporarily unavailable"
- Other: shows error message

Simplified withDb re-throw check: anything with a "status" property
is treated as a React Router response (covers Response and
ErrorResponseImpl). DB errors throw a plain Response(503) that
the error boundary renders nicely.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ullrich Schäfer 2026-03-25 00:06:10 +01:00
parent 83009c2b9b
commit dee6f2806f
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9
3 changed files with 69 additions and 10 deletions

View file

@ -1,5 +1,6 @@
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from "react-router";
import { Links, Meta, Outlet, Scripts, ScrollRestoration, isRouteErrorResponse } from "react-router";
import type { LinksFunction } from "react-router";
import type { Route } from "./+types/root";
import stylesheet from "@trails-cool/ui/styles.css?url";
export const links: LinksFunction = () => [{ rel: "stylesheet", href: stylesheet }];
@ -25,3 +26,33 @@ export function Layout({ children }: { children: React.ReactNode }) {
export default function App() {
return <Outlet />;
}
export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
if (isRouteErrorResponse(error)) {
return (
<div className="mx-auto max-w-md px-4 py-16 text-center">
<h1 className="text-4xl font-bold text-gray-900">{error.status}</h1>
<p className="mt-2 text-gray-600">
{error.status === 404 && "Page not found"}
{error.status === 503 && "Service temporarily unavailable. Please try again later."}
{error.status !== 404 && error.status !== 503 && (error.statusText || "Something went wrong")}
</p>
<a href="/" className="mt-6 inline-block text-blue-600 hover:underline">
Go home
</a>
</div>
);
}
return (
<div className="mx-auto max-w-md px-4 py-16 text-center">
<h1 className="text-4xl font-bold text-gray-900">Error</h1>
<p className="mt-2 text-gray-600">
{error instanceof Error ? error.message : "An unexpected error occurred"}
</p>
<a href="/" className="mt-6 inline-block text-blue-600 hover:underline">
Go home
</a>
</div>
);
}

View file

@ -1,5 +1,6 @@
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from "react-router";
import { Links, Meta, Outlet, Scripts, ScrollRestoration, isRouteErrorResponse } from "react-router";
import type { LinksFunction } from "react-router";
import type { Route } from "./+types/root";
import stylesheet from "@trails-cool/ui/styles.css?url";
export const links: LinksFunction = () => [{ rel: "stylesheet", href: stylesheet }];
@ -25,3 +26,31 @@ export function Layout({ children }: { children: React.ReactNode }) {
export default function App() {
return <Outlet />;
}
export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
if (isRouteErrorResponse(error)) {
return (
<div className="flex h-full items-center justify-center">
<div className="text-center">
<h1 className="text-4xl font-bold text-gray-900">{error.status}</h1>
<p className="mt-2 text-gray-600">
{error.status === 404 && "Page not found"}
{error.status === 503 && "Service temporarily unavailable"}
{error.status !== 404 && error.status !== 503 && (error.statusText || "Something went wrong")}
</p>
</div>
</div>
);
}
return (
<div className="flex h-full items-center justify-center">
<div className="text-center">
<h1 className="text-4xl font-bold text-gray-900">Error</h1>
<p className="mt-2 text-gray-600">
{error instanceof Error ? error.message : "An unexpected error occurred"}
</p>
</div>
</div>
);
}

View file

@ -20,23 +20,22 @@ export type Database = ReturnType<typeof createDb>;
*/
export function withDb<T>(handler: () => Promise<T>): Promise<T> {
return handler().catch((error) => {
// Re-throw React Router responses (redirects, data() throws)
// Check for Response, ErrorResponseImpl, or anything with a status property
// Re-throw anything that looks like a React Router response:
// - Response (redirects)
// - ErrorResponseImpl from data() throws (has status + data)
if (
error instanceof Response ||
(error && typeof error === "object" && "status" in error && "data" in error)
(error != null && typeof error === "object" && "status" in error)
) {
throw error;
}
// Any other error from a DB-wrapped handler is treated as DB unavailable
// Database error — throw as a 503 that the error boundary will catch
const message = error instanceof Error ? error.message : String(error);
console.error("[withDb] Database error:", message);
throw new Response(JSON.stringify({ error: "Database unavailable" }), {
status: 503,
headers: { "Content-Type": "application/json" },
});
// Use the same shape as data() throw so isRouteErrorResponse works
throw new Response("Database unavailable", { status: 503, statusText: "Service Unavailable" });
});
}