This commit is contained in:
ct
2025-06-17 20:34:56 +08:00
parent f3e46530c3
commit 72cd226082
26 changed files with 276 additions and 3 deletions

View File

@@ -3,6 +3,8 @@ import '../css/app.css';
import { createInertiaApp } from '@inertiajs/react';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import { createRoot } from 'react-dom/client';
import { ErrorBoundary } from 'react-error-boundary';
import DetailedErrorFallback from './components/DetailedErrorFallback'; // Import your component
import { initializeTheme } from './hooks/use-appearance';
import { AxiosProvider } from './plugins/AxiosContext';
import { MittProvider } from './plugins/MittContext';
@@ -16,13 +18,26 @@ createInertiaApp({
const root = createRoot(el);
const app = (
<>
<ErrorBoundary
FallbackComponent={DetailedErrorFallback}
onError={(error, errorInfo) => {
// Log to console for debugging
console.error('Error caught by boundary:', error, errorInfo);
// You could also send to an error reporting service here
// e.g., Sentry, LogRocket, etc.
}}
onReset={() => {
// Optional: Clear any error state in your app
console.log('Error boundary reset');
}}
>
<MittProvider>
<AxiosProvider>
<App {...props} />
</AxiosProvider>
</MittProvider>
</>
</ErrorBoundary>
);
root.render(app);
@@ -32,5 +47,4 @@ createInertiaApp({
},
});
// This will set light / dark mode on load...
initializeTheme();

View File

@@ -0,0 +1,71 @@
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { AlertCircle } from 'lucide-react';
function DetailedErrorFallback({ error, resetErrorBoundary }) {
return (
<div className="min-h-screen bg-gray-50 p-8">
<Alert variant="destructive" className="mx-auto max-w-4xl">
<AlertCircle className="h-5 w-5" />
<AlertTitle className="mb-2 text-xl font-bold">Runtime Error</AlertTitle>
<AlertDescription>
<div className="mt-4 space-y-4">
{/* Error Message */}
<div>
<h3 className="text-sm font-medium text-gray-500">Error Message:</h3>
<p className="mt-1 font-medium text-red-600">{error.message}</p>
</div>
{/* Component Stack */}
<div>
<h3 className="text-sm font-medium text-gray-500">Component Stack:</h3>
<pre className="mt-1 overflow-auto rounded-md bg-gray-900 p-4 text-sm text-white">
{error.componentStack || error.stack}
</pre>
</div>
{/* Additional Error Info */}
{error.fileName && (
<div>
<h3 className="text-sm font-medium text-gray-500">File:</h3>
<p className="mt-1 font-mono text-sm">{error.fileName}</p>
</div>
)}
{error.lineNumber && (
<div>
<h3 className="text-sm font-medium text-gray-500">Line:</h3>
<p className="mt-1 font-mono text-sm">{error.lineNumber}</p>
</div>
)}
{/* Error Properties */}
<div>
<h3 className="text-sm font-medium text-gray-500">Additional Details:</h3>
<pre className="mt-1 overflow-auto rounded-md bg-gray-100 p-4 text-sm">
{JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}
</pre>
</div>
{/* Actions */}
<div className="mt-6 flex gap-4">
<button
onClick={resetErrorBoundary}
className="rounded bg-red-600 px-4 py-2 text-white transition-colors hover:bg-red-700"
>
Try Again
</button>
<button
onClick={() => window.location.reload()}
className="rounded border border-red-600 px-4 py-2 text-red-600 transition-colors hover:bg-red-50"
>
Reload Page
</button>
</div>
</div>
</AlertDescription>
</Alert>
</div>
);
}
export default DetailedErrorFallback;