2022-06-11 03:08:49 +00:00
|
|
|
import { Component, ErrorInfo, ReactNode } from "react";
|
|
|
|
import { ViewNotice } from "../views/notice";
|
2022-07-19 17:51:14 +00:00
|
|
|
import { ErrorBlock, ErrorInline, ErrorMain, ErrorView } from "./renderers";
|
2022-06-11 03:08:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
export type ErrorBoundaryProps = {
|
|
|
|
text: string,
|
|
|
|
children: ReactNode,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export type ErrorBoundaryState = {
|
|
|
|
error?: Error,
|
|
|
|
errorInfo?: ErrorInfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error boundary component which displays a {@link ViewNotice} with an {@link ErrorBlock} containing the occurred error inside.
|
|
|
|
*
|
|
|
|
* To be used in `pages/_app`.
|
|
|
|
*/
|
2022-07-19 17:51:14 +00:00
|
|
|
export class ErrorBoundaryView extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
2022-06-11 03:08:49 +00:00
|
|
|
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 (
|
2022-07-19 17:51:14 +00:00
|
|
|
<ErrorView text={this.props.text} error={this.state.error} />
|
2022-06-11 03:08:49 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return this.props.children
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error boundary component which displays an {@link ErrorBlock} containing the occurred error inside.
|
|
|
|
*
|
|
|
|
* To be used in other components.
|
|
|
|
*/
|
2022-07-18 22:42:56 +00:00
|
|
|
export class ErrorBoundaryBlock extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
2022-06-11 03:08:49 +00:00
|
|
|
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 (
|
|
|
|
<ErrorBlock text={this.props.text} error={this.state.error} />
|
|
|
|
)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return this.props.children
|
|
|
|
}
|
|
|
|
}
|
2022-07-19 17:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error boundary component which displays an {@link ErrorInline} containing the occurred error inside.
|
|
|
|
*
|
|
|
|
* To be used in other components.
|
|
|
|
*/
|
|
|
|
export class ErrorBoundaryInline 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 (
|
|
|
|
<ErrorInline text={this.props.text} error={this.state.error} />
|
|
|
|
)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return this.props.children
|
|
|
|
}
|
|
|
|
}
|
2022-06-11 03:08:49 +00:00
|
|
|
}
|