Skip to content

Commit

Permalink
[+] added an errorElement on router-dom to avoid the app from crashin…
Browse files Browse the repository at this point in the history
…g completely if only a component has an issue
  • Loading branch information
radumojic committed Sep 20, 2023
1 parent 01d48ff commit 3cf04d9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/components/ErrorElement/ErrorElement.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useLocation, useRouteError } from 'react-router-dom';
import { PageState } from 'components';
import { analytics } from 'helpers';
import { faTimes } from 'icons/regular';

export const ErrorElement = () => {
const { pathname } = useLocation();
const explorerVersion = process.env.VITE_APP_CACHE_BUST;
const error = useRouteError();
console.error(error);

if (explorerVersion !== undefined) {
analytics.sendEvent({
action: 'error-encountered',
label: pathname,
explorerVersion
});
}

return (
<PageState
icon={faTimes}
title='Unexpected Error'
description={
<div className='px-spacer'>
<div className='text-break-all text-neutral-500'>
Something went wrong. We are looking into the incident.
</div>
<button
className='btn btn-sm btn-primary mx-auto mt-spacer'
onClick={() => {
window.location.reload();
}}
>
Reload
</button>
</div>
}
className='py-spacer m-auto'
dataTestId='errorScreen'
/>
);
};
1 change: 1 addition & 0 deletions src/components/ErrorElement/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ErrorElement';
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RouteObject } from 'react-router-dom';
import { ErrorElement } from 'components/ErrorElement';
import { withPageTitle } from '../helpers/withPageTitle';

import { TitledRouteObject } from '../routes';
Expand All @@ -20,8 +21,12 @@ export const wrapRoutes = (routes: TitledRouteObject[]): RouteObject[] =>

delete route['title'];

route.errorElement = <ErrorElement />;

return route;
}

route.errorElement = <ErrorElement />;

return route;
});

0 comments on commit 3cf04d9

Please sign in to comment.