-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathErrorBoundary.tsx
46 lines (38 loc) · 1.02 KB
/
ErrorBoundary.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
import type { ErrorInfo, PropsWithChildren } from 'react'
import React from 'react'
import styled from 'styled-components'
const Title = styled.h1`
color: ${({ theme }) => theme.dangerColor};
`
interface ErrorBoundaryProps {
hasError?: boolean
error?: unknown
}
class ErrorBoundary extends React.Component<
PropsWithChildren<ErrorBoundaryProps>,
{ hasError: boolean }
> {
constructor(props: PropsWithChildren<ErrorBoundaryProps>) {
super(props)
this.state = { hasError: this.props.hasError ?? false }
}
static getDerivedStateFromError() {
return { hasError: true }
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error('[ErrorBoundary]', error, errorInfo)
}
render() {
if (this.state.hasError) {
console.error(this.props.error)
return (
<>
<Title data-testid='editor_error'>Sorry, something went wrong!</Title>
<p>{String(this.props.error)}</p>
</>
)
}
return this.props.children
}
}
export default ErrorBoundary