From 78329a35edd05a8e849420769b6c92dd1dfcfb8f Mon Sep 17 00:00:00 2001 From: Muhammad Ahmad Shahid Date: Mon, 5 May 2025 22:10:58 +0500 Subject: [PATCH] Feat: Error Boundary to Application --- app/root.tsx | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/app/root.tsx b/app/root.tsx index 7ffa1a3..9954f01 100644 --- a/app/root.tsx +++ b/app/root.tsx @@ -1,10 +1,20 @@ -import { Link, Links, Meta, NavLink, Outlet, Scripts, ScrollRestoration } from '@remix-run/react'; +import { + isRouteErrorResponse, + Link, + Links, + Meta, + NavLink, + Outlet, + Scripts, + ScrollRestoration, + useRouteError, +} from '@remix-run/react'; import type { LinksFunction } from '@remix-run/node'; import './tailwind.css'; import { Logo } from '~/components/Logo'; import { ArrowUpRight, History, Search } from 'lucide-react'; -import { useEffect } from 'react'; +import { useEffect, useState } from 'react'; export const links: LinksFunction = () => []; @@ -42,6 +52,41 @@ const nav = [ }, ]; +export function ErrorBoundary() { + const error = useRouteError(); + console.error(error); + const [trying, setTrying] = useState(false); + const handleClick = () => { + setTrying(true); + window.location.reload(); + }; + return ( +
+
+

+ Oops! Something went wrong +

+ +

+ {isRouteErrorResponse(error) + ? `${error.status} - ${error.statusText}` + : error instanceof Error + ? error.message + : 'An unexpected error occurred.'} +

+ + +
+
+ ); +} + export function Layout({ children }: { children: React.ReactNode }) { useEffect(() => { document.cookie = `tz=${Intl.DateTimeFormat().resolvedOptions().timeZone}; path=/; max-age=31536000; SameSite=Lax`;