Spaces:
Sleeping
Sleeping
File size: 1,163 Bytes
4e8e113 | 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 41 42 | // src/components/ErrorBoundary.jsx
import { Component } from 'react';
export default class ErrorBoundary extends Component {
constructor(props) {
super(props);
this.state = { hasError: false, message: '' };
}
static getDerivedStateFromError(err) {
return { hasError: true, message: err?.message ?? String(err) };
}
componentDidCatch(err, info) {
console.error('ErrorBoundary caught:', err, info);
}
render() {
if (this.state.hasError) {
return (
<div className="error-banner" style={{ margin: 16 }}>
<span>⚠️</span>
<div>
<strong>Component error</strong>
<div style={{ fontSize: 11, marginTop: 4, fontFamily: 'JetBrains Mono' }}>
{this.state.message}
</div>
</div>
<button
onClick={() => this.setState({ hasError: false, message: '' })}
style={{ marginLeft: 'auto', background: 'none', border: 'none',
color: '#fca5a5', cursor: 'pointer', fontSize: 18 }}
>
↺
</button>
</div>
);
}
return this.props.children;
}
}
|