Skip to content

Commit c34b3da

Browse files
Merge pull request #779 from sudhanshutech/mprove/error
Improve error boundary
2 parents 5e86e48 + a07edba commit c34b3da

File tree

1 file changed

+57
-5
lines changed

1 file changed

+57
-5
lines changed

src/custom/ErrorBoundary/ErrorBoundary.tsx

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const StyledLink = styled(Link)(({ theme }) => ({
2020
}));
2121

2222
const CodeMessage = styled('div')(({ theme }) => ({
23+
display: 'flex',
24+
flexDirection: 'column',
2325
backgroundColor: theme.palette.background.code,
2426
color: theme.palette.text.tertiary,
2527
padding: '.85rem',
@@ -30,14 +32,32 @@ const CodeMessage = styled('div')(({ theme }) => ({
3032
interface FallbackComponentProps extends FallbackProps {
3133
resetErrorBoundary: () => void;
3234
children?: React.ReactNode;
35+
pageUrl?: string;
36+
timestamp?: string;
37+
showPackageInfo?: boolean;
38+
version?: string;
3339
}
3440

35-
export function Fallback({ error, children }: FallbackComponentProps): JSX.Element {
41+
export function Fallback({
42+
error,
43+
children,
44+
showPackageInfo,
45+
version
46+
}: FallbackComponentProps): JSX.Element {
3647
return (
3748
<div role="alert">
3849
<h2>Uh-oh!😔 Please pardon the mesh.</h2>
3950
<CodeMessage>
40-
<code>{(error as Error).message}</code>
51+
<code>
52+
<strong>Error: </strong>
53+
{(error as Error).message}
54+
</code>
55+
<br />
56+
{showPackageInfo && (
57+
<>
58+
<strong>Version:</strong> {version}
59+
</>
60+
)}
4161
</CodeMessage>
4262
<ErrorMessage>
4363
We apologize for the inconvenience. The issue may be on our end. If troubleshooting doesn't
@@ -56,18 +76,50 @@ export function Fallback({ error, children }: FallbackComponentProps): JSX.Eleme
5676
}
5777

5878
const reportError = (error: Error, info: React.ErrorInfo): void => {
79+
const pageUrl = window.location.href;
80+
const timestamp = new Date().toLocaleString();
5981
// This is where you'd send the error to Sentry, etc
60-
console.log('Error Caught Inside Boundary --reportError', error, 'Info', info);
82+
console.log(
83+
'Error Caught Inside Boundary --reportError',
84+
error,
85+
'Info',
86+
info,
87+
'Page URL:',
88+
pageUrl,
89+
'Timestamp:',
90+
timestamp
91+
);
6192
};
6293

6394
interface ErrorBoundaryProps {
6495
customFallback?: React.ComponentType<FallbackProps>;
6596
children: React.ReactNode;
97+
onErrorCaught?: (error: string) => void;
6698
}
6799

68-
export const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({ customFallback, children }) => {
100+
export const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({
101+
customFallback,
102+
children,
103+
onErrorCaught
104+
}) => {
105+
const pageUrl = window.location.href;
106+
const timestamp = new Date().toLocaleString();
107+
108+
const handleError = (error: Error, info: React.ErrorInfo) => {
109+
// Pass error message to onErrorCaught
110+
onErrorCaught?.(error.message);
111+
reportError(error, info);
112+
};
113+
69114
return (
70-
<ReactErrorBoundary FallbackComponent={customFallback ?? Fallback} onError={reportError}>
115+
<ReactErrorBoundary
116+
FallbackComponent={
117+
customFallback
118+
? customFallback
119+
: (props) => <Fallback {...props} pageUrl={pageUrl} timestamp={timestamp} />
120+
}
121+
onError={handleError}
122+
>
71123
{children}
72124
</ReactErrorBoundary>
73125
);

0 commit comments

Comments
 (0)