Files
Inventory-Manager-CC/web/client/src/components/ErrorBoundary.jsx

55 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import React from 'react';
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
componentDidCatch(error, errorInfo) {
console.error('ErrorBoundary caught:', error, errorInfo);
}
render() {
if (this.state.hasError) {
return (
<div style={{
padding: '2rem',
background: '#1a1a2e',
color: '#ff6b6b',
borderRadius: '8px',
margin: '1rem',
fontFamily: 'monospace',
}}>
<h2> Something went wrong</h2>
<p style={{ color: '#ccc' }}>
{this.state.error?.message || 'Unknown error'}
</p>
<button
onClick={() => this.setState({ hasError: false, error: null })}
style={{
padding: '0.5rem 1rem',
background: '#4a7c59',
color: 'white',
border: 'none',
borderRadius: '4px',
cursor: 'pointer',
fontFamily: 'inherit',
}}
>
🔄 Try Again
</button>
</div>
);
}
return this.props.children;
}
}
export default ErrorBoundary;