@@ -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,32 @@ 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 ;
37
+ showPackageInfo ?: boolean ;
38
+ version ?: string ;
33
39
}
34
40
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 {
36
47
return (
37
48
< div role = "alert" >
38
49
< h2 > Uh-oh!😔 Please pardon the mesh.</ h2 >
39
50
< 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
+ ) }
41
61
</ CodeMessage >
42
62
< ErrorMessage >
43
63
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
56
76
}
57
77
58
78
const reportError = ( error : Error , info : React . ErrorInfo ) : void => {
79
+ const pageUrl = window . location . href ;
80
+ const timestamp = new Date ( ) . toLocaleString ( ) ;
59
81
// 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
+ ) ;
61
92
} ;
62
93
63
94
interface ErrorBoundaryProps {
64
95
customFallback ?: React . ComponentType < FallbackProps > ;
65
96
children : React . ReactNode ;
97
+ onErrorCaught ?: ( error : string ) => void ;
66
98
}
67
99
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
+
69
114
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
+ >
71
123
{ children }
72
124
</ ReactErrorBoundary >
73
125
) ;
0 commit comments