From 905ef99e0ef7b3799c74b4f7271601a80dd4f701 Mon Sep 17 00:00:00 2001 From: codytseng Date: Sat, 30 Aug 2025 13:53:58 +0800 Subject: [PATCH] feat: add ErrorBoundary component for improved error handling --- src/components/ErrorBoundary.tsx | 78 ++++++++++++++++++++++++++++++++ src/main.tsx | 5 +- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/components/ErrorBoundary.tsx diff --git a/src/components/ErrorBoundary.tsx b/src/components/ErrorBoundary.tsx new file mode 100644 index 0000000..8745855 --- /dev/null +++ b/src/components/ErrorBoundary.tsx @@ -0,0 +1,78 @@ +import { Button } from '@/components/ui/button' +import { RotateCw } from 'lucide-react' +import React, { Component, ReactNode } from 'react' + +interface ErrorBoundaryProps { + children: ReactNode +} + +interface ErrorBoundaryState { + hasError: boolean + error?: Error +} + +export class ErrorBoundary extends Component { + constructor(props: ErrorBoundaryProps) { + super(props) + this.state = { hasError: false } + } + + static getDerivedStateFromError(error: Error) { + return { hasError: true, error } + } + + componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { + console.error('ErrorBoundary caught an error:', error, errorInfo) + } + + render() { + if (this.state.hasError) { + return ( +
+

Oops, something went wrong.

+

+ Sorry for the inconvenience. If you don't mind helping, you can{' '} + + submit an issue on GitHub + {' '} + with the error details, or{' '} + + mention me + + . Thank you for your support! +

+ {this.state.error?.message && ( + <> + +
+                Error: {this.state.error.message}
+              
+ + )} + +
+ ) + } + return this.props.children + } +} diff --git a/src/main.tsx b/src/main.tsx index e7958e5..a77826e 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -6,9 +6,12 @@ import './services/lightning.service' import { StrictMode } from 'react' import { createRoot } from 'react-dom/client' import App from './App.tsx' +import { ErrorBoundary } from './components/ErrorBoundary.tsx' createRoot(document.getElementById('root')!).render( - + + + )