ai_learn_node/frontend/src/components/ErrorBoundary.tsx

49 lines
1.3 KiB
TypeScript
Raw Normal View History

2026-01-14 11:30:15 +00:00
import { Component, ErrorInfo, ReactNode } from 'react';
2026-01-13 01:59:40 +00:00
interface Props {
children: ReactNode;
}
interface State {
hasError: boolean;
error: Error | null;
}
export class ErrorBoundary extends Component<Props, State> {
public state: State = {
hasError: false,
error: null,
};
public static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error('Uncaught error:', error, errorInfo);
}
public render() {
if (this.state.hasError) {
return (
<div className="min-h-screen flex items-center justify-center bg-gray-50">
<div className="max-w-md w-full bg-white shadow-lg rounded-lg p-6">
<h1 className="text-2xl font-bold text-red-600 mb-4"></h1>
<p className="text-gray-700 mb-4">
{this.state.error?.message || '未知错误'}
</p>
<button
onClick={() => window.location.reload()}
className="px-4 py-2 bg-primary-600 text-white rounded hover:bg-primary-700"
>
</button>
</div>
</div>
);
}
return this.props.children;
}
}