-
Notifications
You must be signed in to change notification settings - Fork 26k
/
error-boundary.tsx
154 lines (138 loc) · 3.77 KB
/
error-boundary.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use client'
import React from 'react'
import { usePathname } from './navigation'
const styles = {
error: {
// https://github.com/sindresorhus/modern-normalize/blob/main/modern-normalize.css#L38-L52
fontFamily:
'system-ui,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"',
height: '100vh',
textAlign: 'center',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
},
desc: {
textAlign: 'left',
},
text: {
fontSize: '14px',
fontWeight: 400,
lineHeight: '3em',
margin: 0,
},
} as const
export type ErrorComponent = React.ComponentType<{
error: Error
reset: () => void
}>
export interface ErrorBoundaryProps {
children?: React.ReactNode
errorComponent: ErrorComponent
errorStyles?: React.ReactNode | undefined
}
interface ErrorBoundaryHandlerProps extends ErrorBoundaryProps {
pathname: string
}
interface ErrorBoundaryHandlerState {
error: Error | null
previousPathname: string
}
export class ErrorBoundaryHandler extends React.Component<
ErrorBoundaryHandlerProps,
ErrorBoundaryHandlerState
> {
constructor(props: ErrorBoundaryHandlerProps) {
super(props)
this.state = { error: null, previousPathname: this.props.pathname }
}
static getDerivedStateFromError(error: Error) {
return { error }
}
static getDerivedStateFromProps(
props: ErrorBoundaryHandlerProps,
state: ErrorBoundaryHandlerState
): ErrorBoundaryHandlerState | null {
/**
* Handles reset of the error boundary when a navigation happens.
* Ensures the error boundary does not stay enabled when navigating to a new page.
* Approach of setState in render is safe as it checks the previous pathname and then overrides
* it as outlined in https://react.dev/reference/react/useState#storing-information-from-previous-renders
*/
if (props.pathname !== state.previousPathname && state.error) {
return {
error: null,
previousPathname: props.pathname,
}
}
return {
error: state.error,
previousPathname: props.pathname,
}
}
reset = () => {
this.setState({ error: null })
}
render() {
if (this.state.error) {
return (
<>
{this.props.errorStyles}
<this.props.errorComponent
error={this.state.error}
reset={this.reset}
/>
</>
)
}
return this.props.children
}
}
export default function GlobalError({ error }: { error: any }) {
return (
<html>
<head></head>
<body>
<div style={styles.error}>
<div style={styles.desc}>
<h2 style={styles.text}>
Application error: a client-side exception has occurred (see the
browser console for more information).
</h2>
{error?.digest && (
<p style={styles.text}>{`Digest: ${error.digest}`}</p>
)}
</div>
</div>
</body>
</html>
)
}
/**
* Handles errors through `getDerivedStateFromError`.
* Renders the provided error component and provides a way to `reset` the error boundary state.
*/
/**
* Renders error boundary with the provided "errorComponent" property as the fallback.
* If no "errorComponent" property is provided it renders the children without an error boundary.
*/
export function ErrorBoundary({
errorComponent,
errorStyles,
children,
}: ErrorBoundaryProps & { children: React.ReactNode }): JSX.Element {
const pathname = usePathname()
if (errorComponent) {
return (
<ErrorBoundaryHandler
pathname={pathname}
errorComponent={errorComponent}
errorStyles={errorStyles}
>
{children}
</ErrorBoundaryHandler>
)
}
return <>{children}</>
}