Skip to content

Commit

Permalink
add global error boundary for capturing client-side errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thostetler committed Sep 18, 2024
1 parent cc41457 commit ad80068
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/components/Feedbacks/GlobalErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Center } from '@chakra-ui/react';
import { ErrorBoundary } from '@sentry/nextjs';
import { ReactElement, ReactNode } from 'react';
import { SuspendedAlert } from './SuspendedAlert';

export const GlobalErrorBoundary = ({ children }: { children: ReactNode | ReactElement }) => {
return (
<ErrorBoundary
fallback={({ error, resetError }) => (
<Center>
<SuspendedAlert
label="Sorry, we've encountered an application error"
error={error}
resetErrorBoundary={resetError}
/>
</Center>
)}
>
{children}
</ErrorBoundary>
);
};
3 changes: 2 additions & 1 deletion src/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Notification } from '@/components/Notification';
import Head from 'next/head';
import { Favicons } from '@/components/Favicons/Favicons';
import { BRAND_NAME_FULL } from '@/config';
import { GlobalErrorBoundary } from '@/components/Feedbacks/GlobalErrorBoundary';

const LandingTabs = dynamic(() => import('@/components/LandingTabs/LandingTabs').then((mod) => mod.LandingTabs), {
ssr: false,
Expand Down Expand Up @@ -38,7 +39,7 @@ export const Layout: FC = ({ children }) => {
<main>
{isLandingPage && <LandingTabs />}
<Container maxW={isLandingPage ? 'container.md' : 'container.xl'} id="main-content">
{children}
<GlobalErrorBoundary>{children}</GlobalErrorBoundary>
</Container>
</main>
{isPrint ? null : <Footer />}
Expand Down
15 changes: 12 additions & 3 deletions src/pages/search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ const SearchPage: NextPage = () => {

// generate search string and trigger page transition, also update store
const search = makeSearchParams({ ...params, ...query, sort, p: 1 });
void router.push({ pathname: router.pathname, search }, null, { scroll: false, shallow: true });
void router.push({ pathname: router.pathname, search }, null, {
scroll: false,
shallow: true,
});
};

// On submission handler
Expand All @@ -165,7 +168,10 @@ const SearchPage: NextPage = () => {

// generate a URL search string and trigger a page transition, and update store
const search = makeSearchParams({ ...params, ...query, q, p: 1 });
void router.push({ pathname: router.pathname, search }, null, { scroll: false, shallow: true });
void router.push({ pathname: router.pathname, search }, null, {
scroll: false,
shallow: true,
});
};

// Update the store when we have data
Expand Down Expand Up @@ -193,7 +199,10 @@ const SearchPage: NextPage = () => {
// clear current docs on filter change
clearSelectedDocs();

void router.push({ pathname: router.pathname, search }, null, { scroll: false, shallow: true });
void router.push({ pathname: router.pathname, search }, null, {
scroll: false,
shallow: true,
});
};

const handleToggleExpand = () => {
Expand Down

0 comments on commit ad80068

Please sign in to comment.