'use client';
import React, { Component, ErrorInfo, ReactNode } from 'react';
interface Props {
children: ReactNode;
fallback?: ReactNode;
}
interface State {
hasError: boolean;
error?: Error;
}
/**
* Error Boundary component that catches JavaScript errors anywhere in the child component tree
* Logs the errors and displays a fallback UI instead of crashing the entire application
*/
export class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error: Error): State {
// Update state so the next render will show the fallback UI
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
// Log error details to console (in production, you might send to error tracking service)
console.error('ErrorBoundary caught an error:', error, errorInfo);
}
render() {
if (this.state.hasError) {
// Custom fallback UI if provided
if (this.props.fallback) {
return this.props.fallback;
}
// Default fallback UI
return (
<div className="min-h-screen flex items-center justify-center bg-gray-50 px-4">
<div className="max-w-md w-full bg-white rounded-lg shadow-lg p-8 text-center">
<div className="mx-auto flex items-center justify-center h-12 w-12 rounded-full bg-red-100 mb-4">
<svg
className="h-6 w-6 text-red-600"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
/>
</svg>
</div>
<h2 className="text-2xl font-bold text-gray-900 mb-2">
Something went wrong
</h2>
<p className="text-gray-600 mb-6">
We encountered an unexpected error. Please try refreshing the page.
</p>
{this.state.error && process.env.NODE_ENV === 'development' && (
<details className="text-left mb-6">
<summary className="cursor-pointer text-sm font-medium text-gray-700 mb-2">
Error details (development only)
</summary>
<pre className="text-xs bg-gray-100 p-3 rounded overflow-x-auto text-red-600">
{this.state.error.toString()}
{'\n\n'}
{this.state.error.stack}
</pre>
</details>
)}
<button
onClick={() => window.location.reload()}
className="w-full bg-red-600 text-white px-4 py-2 rounded-md hover:bg-red-700 transition-colors"
>
Refresh Page
</button>
</div>
</div>
);
}
return this.props.children;
}
}