import { Component, ErrorInfo, ReactNode } from 'react'; import { Logger } from 'simple-logging-system'; const logger: Logger = new Logger('GlobalErrorBoundary'); type GlobalErrorBoundaryProps = { children: ReactNode, }; type GlobalErrorBoundaryState = { error?: Error, errorInfo?: ErrorInfo, }; export default class GlobalErrorBoundary extends Component { constructor(props: GlobalErrorBoundaryProps) { super(props); this.state = { error: undefined, errorInfo: undefined }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { this.setState({ error, errorInfo, }); logger.error( `${error.name}: ${error.message}`, { stack: error.stack, componentStack: errorInfo.componentStack }, ); } render() { const { error, errorInfo } = this.state; const { children } = this.props; if (errorInfo) { return (

Something went wrong.

You can try to reload the page, hopefully the bug will not occur again!
{error && error.toString()}
{errorInfo.componentStack}
); } return children; } }