Skip to content

Commit

Permalink
Merge pull request #779 from sudhanshutech/mprove/error
Browse files Browse the repository at this point in the history
Improve error boundary
  • Loading branch information
sudhanshutech authored Nov 4, 2024
2 parents 5e86e48 + a07edba commit c34b3da
Showing 1 changed file with 57 additions and 5 deletions.
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

0 comments on commit c34b3da

Please sign in to comment.