@@ -20,6 +20,8 @@ const StyledLink = styled(Link)(({ theme }) => ({
20
20
} ) ) ;
21
21
22
22
const CodeMessage = styled ( 'div' ) ( ( { theme } ) => ( {
23
+ display : 'flex' ,
24
+ flexDirection : 'column' ,
23
25
backgroundColor : theme . palette . background . code ,
24
26
color : theme . palette . text . tertiary ,
25
27
padding : '.85rem' ,
@@ -30,14 +32,25 @@ const CodeMessage = styled('div')(({ theme }) => ({
30
32
interface FallbackComponentProps extends FallbackProps {
31
33
resetErrorBoundary : ( ) => void ;
32
34
children ?: React . ReactNode ;
35
+ pageUrl ?: string ;
36
+ timestamp ?: string ;
33
37
}
34
38
35
39
export function Fallback ( { error, children } : FallbackComponentProps ) : JSX . Element {
40
+ const pageUrl = window . location . href ;
41
+ const timestamp = new Date ( ) . toLocaleString ( ) ;
36
42
return (
37
43
< div role = "alert" >
38
44
< h2 > Uh-oh!😔 Please pardon the mesh.</ h2 >
39
45
< 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 }
41
54
</ CodeMessage >
42
55
< ErrorMessage >
43
56
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
56
69
}
57
70
58
71
const reportError = ( error : Error , info : React . ErrorInfo ) : void => {
72
+ const pageUrl = window . location . href ;
73
+ const timestamp = new Date ( ) . toLocaleString ( ) ;
59
74
// 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
+ ) ;
61
85
} ;
62
86
63
87
interface ErrorBoundaryProps {
64
88
customFallback ?: React . ComponentType < FallbackProps > ;
65
89
children : React . ReactNode ;
90
+ onErrorCaught ?: ( error : string ) => void ;
66
91
}
67
92
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
+
69
107
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
+ >
71
116
{ children }
72
117
</ ReactErrorBoundary >
73
118
) ;
0 commit comments