Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error boundary #779

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions src/custom/ErrorBoundary/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const StyledLink = styled(Link)(({ theme }) => ({
}));

const CodeMessage = styled('div')(({ theme }) => ({
display: 'flex',
flexDirection: 'column',
backgroundColor: theme.palette.background.code,
color: theme.palette.text.tertiary,
padding: '.85rem',
Expand All @@ -30,14 +32,32 @@ const CodeMessage = styled('div')(({ theme }) => ({
interface FallbackComponentProps extends FallbackProps {
resetErrorBoundary: () => void;
children?: React.ReactNode;
pageUrl?: string;
timestamp?: string;
showPackageInfo?: boolean;
version?: string;
}

export function Fallback({ error, children }: FallbackComponentProps): JSX.Element {
export function Fallback({
error,
children,
showPackageInfo,
version
}: FallbackComponentProps): JSX.Element {
return (
<div role="alert">
<h2>Uh-oh!😔 Please pardon the mesh.</h2>
<CodeMessage>
<code>{(error as Error).message}</code>
<code>
<strong>Error: </strong>
{(error as Error).message}
</code>
<br />
{showPackageInfo && (
<>
<strong>Version:</strong> {version}
</>
)}
</CodeMessage>
<ErrorMessage>
We apologize for the inconvenience. The issue may be on our end. If troubleshooting doesn't
Expand All @@ -56,18 +76,50 @@ export function Fallback({ error, children }: FallbackComponentProps): JSX.Eleme
}

const reportError = (error: Error, info: React.ErrorInfo): void => {
const pageUrl = window.location.href;
const timestamp = new Date().toLocaleString();
// This is where you'd send the error to Sentry, etc
console.log('Error Caught Inside Boundary --reportError', error, 'Info', info);
console.log(
'Error Caught Inside Boundary --reportError',
error,
'Info',
info,
'Page URL:',
pageUrl,
'Timestamp:',
timestamp
);
};

interface ErrorBoundaryProps {
customFallback?: React.ComponentType<FallbackProps>;
children: React.ReactNode;
onErrorCaught?: (error: string) => void;
}

export const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({ customFallback, children }) => {
export const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({
customFallback,
children,
onErrorCaught
}) => {
const pageUrl = window.location.href;
const timestamp = new Date().toLocaleString();

const handleError = (error: Error, info: React.ErrorInfo) => {
// Pass error message to onErrorCaught
onErrorCaught?.(error.message);
reportError(error, info);
};

return (
<ReactErrorBoundary FallbackComponent={customFallback ?? Fallback} onError={reportError}>
<ReactErrorBoundary
FallbackComponent={
customFallback
? customFallback
: (props) => <Fallback {...props} pageUrl={pageUrl} timestamp={timestamp} />
}
onError={handleError}
>
{children}
</ReactErrorBoundary>
);
Expand Down
Loading