Skip to content

Commit 98b56ac

Browse files
committed
fix(error): boundary
Signed-off-by: Sudhanshu Dasgupta <[email protected]>
1 parent e35096e commit 98b56ac

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

src/custom/ErrorBoundary/ErrorBoundary.tsx

Lines changed: 49 additions & 4 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,25 @@ const CodeMessage = styled('div')(({ theme }) => ({
3032
interface FallbackComponentProps extends FallbackProps {
3133
resetErrorBoundary: () => void;
3234
children?: React.ReactNode;
35+
pageUrl?: string;
36+
timestamp?: string;
3337
}
3438

3539
export function Fallback({ error, children }: FallbackComponentProps): JSX.Element {
40+
const pageUrl = window.location.href;
41+
const timestamp = new Date().toLocaleString();
3642
return (
3743
<div role="alert">
3844
<h2>Uh-oh!😔 Please pardon the mesh.</h2>
3945
<CodeMessage>
40-
<code>{(error as Error).message}</code>
46+
<code>
47+
<strong>Error: </strong>
48+
{(error as Error).message}
49+
</code>
50+
<br />
51+
<strong> URL: </strong> {pageUrl}
52+
<br />
53+
<strong>Logged at:</strong> {timestamp}
4154
</CodeMessage>
4255
<ErrorMessage>
4356
We apologize for the inconvenience. The issue may be on our end. If troubleshooting doesn't
@@ -56,18 +69,50 @@ export function Fallback({ error, children }: FallbackComponentProps): JSX.Eleme
5669
}
5770

5871
const reportError = (error: Error, info: React.ErrorInfo): void => {
72+
const pageUrl = window.location.href;
73+
const timestamp = new Date().toLocaleString();
5974
// This is where you'd send the error to Sentry, etc
60-
console.log('Error Caught Inside Boundary --reportError', error, 'Info', info);
75+
console.log(
76+
'Error Caught Inside Boundary --reportError',
77+
error,
78+
'Info',
79+
info,
80+
'Page URL:',
81+
pageUrl,
82+
'Timestamp:',
83+
timestamp
84+
);
6185
};
6286

6387
interface ErrorBoundaryProps {
6488
customFallback?: React.ComponentType<FallbackProps>;
6589
children: React.ReactNode;
90+
onErrorCaught?: (error: string) => void;
6691
}
6792

68-
export const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({ customFallback, children }) => {
93+
export const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({
94+
customFallback,
95+
children,
96+
onErrorCaught
97+
}) => {
98+
const pageUrl = window.location.href;
99+
const timestamp = new Date().toLocaleString();
100+
101+
const handleError = (error: Error, info: React.ErrorInfo) => {
102+
// Pass error message to onErrorCaught
103+
onErrorCaught?.(error.message);
104+
reportError(error, info);
105+
};
106+
69107
return (
70-
<ReactErrorBoundary FallbackComponent={customFallback ?? Fallback} onError={reportError}>
108+
<ReactErrorBoundary
109+
FallbackComponent={
110+
customFallback
111+
? customFallback
112+
: (props) => <Fallback {...props} pageUrl={pageUrl} timestamp={timestamp} />
113+
}
114+
onError={handleError}
115+
>
71116
{children}
72117
</ReactErrorBoundary>
73118
);

0 commit comments

Comments
 (0)