68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
|
|
// src/components/ErrorBoundary.tsx
|
||
|
|
import React, { Component, ErrorInfo, ReactNode } from 'react';
|
||
|
|
import { AlertTriangle } from 'lucide-react';
|
||
|
|
|
||
|
|
interface Props {
|
||
|
|
children: ReactNode;
|
||
|
|
}
|
||
|
|
|
||
|
|
interface State {
|
||
|
|
hasError: boolean;
|
||
|
|
error?: Error;
|
||
|
|
}
|
||
|
|
|
||
|
|
class ErrorBoundary extends Component<Props, State> {
|
||
|
|
public state: State = {
|
||
|
|
hasError: false
|
||
|
|
};
|
||
|
|
|
||
|
|
public static getDerivedStateFromError(error: Error): State {
|
||
|
|
return { hasError: true, error };
|
||
|
|
}
|
||
|
|
|
||
|
|
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
||
|
|
console.error('ErrorBoundary caught an error:', error, errorInfo);
|
||
|
|
}
|
||
|
|
|
||
|
|
public render() {
|
||
|
|
if (this.state.hasError) {
|
||
|
|
return (
|
||
|
|
<div className="min-h-screen flex items-center justify-center bg-gray-50 px-4">
|
||
|
|
<div className="max-w-md w-full text-center">
|
||
|
|
<div className="bg-white rounded-2xl p-8 shadow-strong">
|
||
|
|
<div className="mx-auto h-16 w-16 bg-red-100 rounded-full flex items-center justify-center mb-6">
|
||
|
|
<AlertTriangle className="h-8 w-8 text-red-600" />
|
||
|
|
</div>
|
||
|
|
<h1 className="text-xl font-bold text-gray-900 mb-4">
|
||
|
|
¡Oops! Algo salió mal
|
||
|
|
</h1>
|
||
|
|
<p className="text-gray-600 mb-6">
|
||
|
|
Ha ocurrido un error inesperado. Por favor, recarga la página.
|
||
|
|
</p>
|
||
|
|
<button
|
||
|
|
onClick={() => window.location.reload()}
|
||
|
|
className="w-full bg-primary-500 text-white py-3 px-4 rounded-xl font-medium hover:bg-primary-600 transition-colors"
|
||
|
|
>
|
||
|
|
Recargar página
|
||
|
|
</button>
|
||
|
|
{process.env.NODE_ENV === 'development' && (
|
||
|
|
<details className="mt-4 text-left">
|
||
|
|
<summary className="text-sm text-gray-500 cursor-pointer">
|
||
|
|
Detalles del error
|
||
|
|
</summary>
|
||
|
|
<pre className="mt-2 text-xs text-red-600 bg-red-50 p-2 rounded overflow-auto">
|
||
|
|
{this.state.error?.stack}
|
||
|
|
</pre>
|
||
|
|
</details>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return this.props.children;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export default ErrorBoundary;
|