aboutsummaryrefslogtreecommitdiff
path: root/modern/src/ErrorBoundary.js
blob: 5a24435f709af6907566f5944edeb2f048653efc (plain)
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
import React from 'react';
import { Alert } from '@mui/material';

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      error: null,
    };
  }

  static getDerivedStateFromError(error) {
    return { error };
  }

  componentDidCatch(error, errorInfo) {
    console.log(error);
    console.log(errorInfo);
  }

  render() {
    const { error } = this.state;
    if (error) {
      return (
        <Alert severity="error">
          <code
            dangerouslySetInnerHTML={{
              __html: error.stack.replaceAll('\n', '<br>').replaceAll(' ', '&nbsp;'),
            }}
          />
        </Alert>
      );
    }
    const { children } = this.props;
    return children;
  }
}

export default ErrorBoundary;