2022-06-04 03:13:19 +00:00
|
|
|
import { Component, ErrorInfo, ReactNode } from "react";
|
2022-06-05 15:35:35 +00:00
|
|
|
import { ViewNotice } from "../view/ViewNotice";
|
2022-06-04 03:13:19 +00:00
|
|
|
import { ErrorBlock } from "./ErrorBlock";
|
|
|
|
|
|
|
|
type ErrorBoundaryProps = {
|
|
|
|
text: string,
|
|
|
|
children: ReactNode,
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorBoundaryState = {
|
|
|
|
error?: Error,
|
|
|
|
errorInfo?: ErrorInfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
|
|
constructor(props: ErrorBoundaryProps) {
|
|
|
|
super(props)
|
|
|
|
this.state = {error: undefined, errorInfo: undefined}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
|
|
this.setState(state => {
|
|
|
|
return {...state, error, errorInfo}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if(this.state.error) {
|
|
|
|
return (
|
|
|
|
<ViewNotice
|
2022-06-05 15:35:35 +00:00
|
|
|
notice={
|
|
|
|
<ErrorBlock text={this.props.text} error={this.state.error}/>
|
|
|
|
}
|
|
|
|
/>
|
2022-06-04 03:13:19 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return this.props.children
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|