UNPKG

ajinkya-mhetre-mern

Version:

A MERN starter with frontend and backend folders

110 lines (98 loc) 4.14 kB
// components/common/ErrorBoundary.jsx import React from 'react' import { AlertTriangle, RefreshCw, Home } from 'lucide-react' class ErrorBoundary extends React.Component { constructor(props) { super(props) this.state = { hasError: false, error: null, errorInfo: null } } static getDerivedStateFromError(error) { // Update state so the next render will show the fallback UI return { hasError: true } } componentDidCatch(error, errorInfo) { // Log the error to console or error reporting service console.error('Error caught by boundary:', error, errorInfo) this.setState({ error: error, errorInfo: errorInfo }) } handleReload = () => { window.location.reload() } handleGoHome = () => { window.location.href = '/home' } render() { if (this.state.hasError) { return ( <div className="min-h-screen bg-gray-50 flex flex-col justify-center items-center px-4"> <div className="max-w-lg w-full text-center"> {/* Error Illustration */} <div className="mb-8"> <div className="w-32 h-32 mx-auto mb-6 bg-red-100 rounded-full flex items-center justify-center"> <AlertTriangle className="w-16 h-16 text-red-500" /> </div> </div> {/* Error Message */} <div className="mb-8"> <h1 className="text-3xl font-bold text-gray-900 mb-4">Something went wrong</h1> <p className="text-lg text-gray-600 mb-2"> We're sorry, but something unexpected happened. </p> <p className="text-gray-500"> The error has been logged and we'll look into it. </p> </div> {/* Action Buttons */} <div className="space-y-4"> <div className="flex flex-col sm:flex-row gap-4 justify-center"> <button onClick={this.handleReload} className="flex items-center justify-center space-x-2 px-6 py-3 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-colors" > <RefreshCw className="w-4 h-4" /> <span>Reload Page</span> </button> <button onClick={this.handleGoHome} className="flex items-center justify-center space-x-2 px-6 py-3 bg-green-600 hover:bg-green-700 text-white rounded-lg transition-colors" > <Home className="w-4 h-4" /> <span>Go Home</span> </button> </div> </div> {/* Error Details (only in development) */} {process.env.NODE_ENV === 'development' && this.state.error && ( <div className="mt-12 pt-8 border-t border-gray-200"> <details className="text-left"> <summary className="cursor-pointer text-sm font-medium text-gray-700 mb-4"> Error Details (Development Only) </summary> <div className="bg-red-50 border border-red-200 rounded-lg p-4 text-xs"> <div className="mb-4"> <strong className="text-red-800">Error:</strong> <pre className="mt-2 text-red-700 whitespace-pre-wrap"> {this.state.error && this.state.error.toString()} </pre> </div> <div> <strong className="text-red-800">Stack Trace:</strong> <pre className="mt-2 text-red-700 whitespace-pre-wrap overflow-auto max-h-64"> {this.state.errorInfo.componentStack} </pre> </div> </div> </details> </div> )} </div> </div> ) } return this.props.children } } export default ErrorBoundary