90 lines
2.7 KiB
TypeScript
90 lines
2.7 KiB
TypeScript
import { Component, ErrorInfo, ReactNode } from 'react';
|
||
import { useErrorHandler } from '../../hooks/useErrorHandler';
|
||
|
||
interface Props {
|
||
children: ReactNode;
|
||
fallback?: ReactNode;
|
||
pageName?: string;
|
||
}
|
||
|
||
interface State {
|
||
hasError: boolean;
|
||
error: Error | null;
|
||
}
|
||
|
||
export class PageErrorBoundary extends Component<Props, State> {
|
||
constructor(props: Props) {
|
||
super(props);
|
||
this.state = {
|
||
hasError: false,
|
||
error: null,
|
||
};
|
||
}
|
||
|
||
static getDerivedStateFromError(error: Error): State {
|
||
return {
|
||
hasError: true,
|
||
error,
|
||
};
|
||
}
|
||
|
||
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
||
console.error(`[${this.props.pageName || 'Page'}] Error:`, error, errorInfo);
|
||
// Error will be caught by GlobalErrorDisplay via useErrorHandler
|
||
}
|
||
|
||
render() {
|
||
if (this.state.hasError) {
|
||
if (this.props.fallback) {
|
||
return this.props.fallback;
|
||
}
|
||
|
||
return (
|
||
<div className="flex items-center justify-center min-h-[400px] p-6">
|
||
<div className="text-center max-w-md">
|
||
<div className="text-6xl mb-4">⚠️</div>
|
||
<h2 className="text-2xl font-semibold text-gray-800 dark:text-white mb-4">
|
||
Something went wrong
|
||
</h2>
|
||
<p className="text-gray-600 dark:text-gray-400 mb-4">
|
||
{this.state.error?.message || 'An unexpected error occurred on this page'}
|
||
</p>
|
||
<div className="space-x-3">
|
||
<button
|
||
onClick={() => {
|
||
this.setState({ hasError: false, error: null });
|
||
window.location.reload();
|
||
}}
|
||
className="px-4 py-2 bg-brand-500 text-white rounded-lg hover:bg-brand-600"
|
||
>
|
||
Reload Page
|
||
</button>
|
||
<button
|
||
onClick={() => {
|
||
this.setState({ hasError: false, error: null });
|
||
}}
|
||
className="px-4 py-2 bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-white rounded-lg hover:bg-gray-300 dark:hover:bg-gray-600"
|
||
>
|
||
Try Again
|
||
</button>
|
||
</div>
|
||
{import.meta.env.DEV && this.state.error && (
|
||
<details className="mt-4 text-left">
|
||
<summary className="cursor-pointer text-sm text-gray-500 dark:text-gray-400">
|
||
Error Details (Dev Mode)
|
||
</summary>
|
||
<pre className="mt-2 text-xs bg-gray-100 dark:bg-gray-800 p-2 rounded overflow-auto max-h-64">
|
||
{this.state.error.stack}
|
||
</pre>
|
||
</details>
|
||
)}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
return this.props.children;
|
||
}
|
||
}
|
||
|