File size: 1,137 Bytes
2582052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Component, type ErrorInfo, type ReactNode } from 'react'

type ScreenErrorBoundaryProps = {
  children: ReactNode
}

type ScreenErrorBoundaryState = {
  error: Error | null
}

export class ScreenErrorBoundary extends Component<ScreenErrorBoundaryProps, ScreenErrorBoundaryState> {
  state: ScreenErrorBoundaryState = { error: null }

  static getDerivedStateFromError(error: Error): ScreenErrorBoundaryState {
    return { error }
  }

  componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    console.error('Screen rendering failed.', error, errorInfo.componentStack)
  }

  private retry = () => {
    this.setState({ error: null })
  }

  render() {
    if (this.state.error) {
      return (
        <section className="panel" role="alert">

          <h2 className="section-title">This screen could not be displayed</h2>

          <p className="muted">{this.state.error.message || 'An unexpected error occurred.'}</p>

          <button className="button button-primary" onClick={this.retry}>Try again</button>

        </section>
      )
    }

    return this.props.children
  }
}