|
| 1 | +import React, { Component, ErrorInfo, PropsWithChildren, ReactNode, useState } from 'react'; |
| 2 | +import Paper from '@material-ui/core/Paper'; |
| 3 | +import Typography from '@material-ui/core/Typography'; |
| 4 | +import Button from '@material-ui/core/Button'; |
| 5 | +import Collapse from '@material-ui/core/Collapse'; |
| 6 | +import { makeStyles } from '@material-ui/core/styles'; |
| 7 | + |
| 8 | +const IS_DEV = process.env.NODE_ENV !== 'production'; |
| 9 | + |
| 10 | +const useStyles = makeStyles((theme) => ({ |
| 11 | + wrapper: { |
| 12 | + display: 'flex', |
| 13 | + alignItems: 'center', |
| 14 | + justifyContent: 'center', |
| 15 | + height: '100%', |
| 16 | + minHeight: '60vh', |
| 17 | + padding: theme.spacing(2), |
| 18 | + }, |
| 19 | + root: { |
| 20 | + padding: theme.spacing(4), |
| 21 | + borderLeft: `4px solid ${theme.palette.error.main}`, |
| 22 | + maxWidth: 560, |
| 23 | + width: '100%', |
| 24 | + }, |
| 25 | + title: { |
| 26 | + color: theme.palette.error.main, |
| 27 | + marginBottom: theme.spacing(1), |
| 28 | + }, |
| 29 | + message: { |
| 30 | + marginBottom: theme.spacing(2), |
| 31 | + color: theme.palette.text.secondary, |
| 32 | + }, |
| 33 | + hint: { |
| 34 | + marginBottom: theme.spacing(2), |
| 35 | + color: theme.palette.text.secondary, |
| 36 | + fontStyle: 'italic', |
| 37 | + }, |
| 38 | + actions: { |
| 39 | + display: 'flex', |
| 40 | + gap: theme.spacing(1), |
| 41 | + alignItems: 'center', |
| 42 | + marginBottom: theme.spacing(1), |
| 43 | + }, |
| 44 | + stack: { |
| 45 | + marginTop: theme.spacing(2), |
| 46 | + padding: theme.spacing(2), |
| 47 | + backgroundColor: theme.palette.grey[100], |
| 48 | + borderRadius: theme.shape.borderRadius, |
| 49 | + overflowX: 'auto', |
| 50 | + fontSize: '0.75rem', |
| 51 | + fontFamily: 'monospace', |
| 52 | + whiteSpace: 'pre-wrap', |
| 53 | + wordBreak: 'break-word', |
| 54 | + }, |
| 55 | + devBadge: { |
| 56 | + display: 'inline-block', |
| 57 | + marginBottom: theme.spacing(2), |
| 58 | + padding: '2px 8px', |
| 59 | + backgroundColor: theme.palette.warning.main, |
| 60 | + color: theme.palette.warning.contrastText, |
| 61 | + borderRadius: theme.shape.borderRadius, |
| 62 | + fontSize: '0.7rem', |
| 63 | + fontWeight: 700, |
| 64 | + letterSpacing: '0.05em', |
| 65 | + textTransform: 'uppercase', |
| 66 | + }, |
| 67 | +})); |
| 68 | + |
| 69 | +const ProdFallback = ({ reset }: { reset: () => void }) => { |
| 70 | + const classes = useStyles(); |
| 71 | + return ( |
| 72 | + <div className={classes.wrapper}> |
| 73 | + <Paper className={classes.root} role='alert' elevation={0} variant='outlined'> |
| 74 | + <Typography variant='h6' className={classes.title}> |
| 75 | + Something went wrong |
| 76 | + </Typography> |
| 77 | + <Typography variant='body2' className={classes.message}> |
| 78 | + An unexpected error occurred. Please try again — if the problem persists, contact your |
| 79 | + administrator. |
| 80 | + </Typography> |
| 81 | + <div className={classes.actions}> |
| 82 | + <Button variant='outlined' size='small' color='primary' onClick={reset}> |
| 83 | + Retry |
| 84 | + </Button> |
| 85 | + <Button size='small' onClick={() => window.location.reload()}> |
| 86 | + Reload page |
| 87 | + </Button> |
| 88 | + </div> |
| 89 | + </Paper> |
| 90 | + </div> |
| 91 | + ); |
| 92 | +}; |
| 93 | + |
| 94 | +const DevFallback = ({ |
| 95 | + error, |
| 96 | + name, |
| 97 | + reset, |
| 98 | +}: { |
| 99 | + error: Error; |
| 100 | + name?: string; |
| 101 | + reset: () => void; |
| 102 | +}) => { |
| 103 | + const classes = useStyles(); |
| 104 | + const [showDetails, setShowDetails] = useState(false); |
| 105 | + const context = name ? ` in ${name}` : ''; |
| 106 | + |
| 107 | + return ( |
| 108 | + <div className={classes.wrapper}> |
| 109 | + <Paper className={classes.root} role='alert' elevation={0} variant='outlined'> |
| 110 | + <div className={classes.devBadge}>dev</div> |
| 111 | + <Typography variant='h6' className={classes.title}> |
| 112 | + Something went wrong{context} |
| 113 | + </Typography> |
| 114 | + <Typography variant='body2' className={classes.message}> |
| 115 | + {error.message} |
| 116 | + </Typography> |
| 117 | + <div className={classes.actions}> |
| 118 | + <Button variant='outlined' size='small' color='primary' onClick={reset}> |
| 119 | + Retry |
| 120 | + </Button> |
| 121 | + {error.stack && ( |
| 122 | + <Button size='small' onClick={() => setShowDetails((v) => !v)}> |
| 123 | + {showDetails ? 'Hide stack trace' : 'Show stack trace'} |
| 124 | + </Button> |
| 125 | + )} |
| 126 | + </div> |
| 127 | + {error.stack && ( |
| 128 | + <Collapse in={showDetails}> |
| 129 | + <pre className={classes.stack}>{error.stack}</pre> |
| 130 | + </Collapse> |
| 131 | + )} |
| 132 | + </Paper> |
| 133 | + </div> |
| 134 | + ); |
| 135 | +}; |
| 136 | + |
| 137 | +type Props = PropsWithChildren<{ |
| 138 | + name?: string; |
| 139 | + fallback?: (error: Error, reset: () => void) => ReactNode; |
| 140 | + onError?: (error: Error, errorInfo: ErrorInfo) => void; |
| 141 | +}>; |
| 142 | + |
| 143 | +type State = { error: Error | undefined }; |
| 144 | + |
| 145 | +export class ErrorBoundary extends Component<Props, State> { |
| 146 | + state: State = { error: undefined }; |
| 147 | + |
| 148 | + static getDerivedStateFromError(error: Error): State { |
| 149 | + return { error }; |
| 150 | + } |
| 151 | + |
| 152 | + componentDidCatch(error: Error, errorInfo: ErrorInfo) { |
| 153 | + this.props.onError?.(error, errorInfo); |
| 154 | + if (IS_DEV) { |
| 155 | + console.error('ErrorBoundary caught:', error, errorInfo); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + reset = () => this.setState({ error: undefined }); |
| 160 | + |
| 161 | + render() { |
| 162 | + const { error } = this.state; |
| 163 | + const { children, fallback, name } = this.props; |
| 164 | + |
| 165 | + if (error) { |
| 166 | + if (fallback) return fallback(error, this.reset); |
| 167 | + return IS_DEV ? ( |
| 168 | + <DevFallback error={error} name={name} reset={this.reset} /> |
| 169 | + ) : ( |
| 170 | + <ProdFallback reset={this.reset} /> |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + return children; |
| 175 | + } |
| 176 | +} |
0 commit comments