feat(logging): add comprehensive error boundary and global error handling (Phase 6)

Implemented complete client-side error handling and reporting system:

Error Boundary Component (components/providers/ErrorBoundary.tsx):
- React Error Boundary to catch component errors
- Automatic error logging to client logger
- Error reporting to server endpoint
- User-friendly fallback UI with error details (dev mode)
- Reset and reload functionality
- Custom fallback support via props

Global Error Handler (lib/utils/global-error-handler.ts):
- Window error event listener for uncaught errors
- Unhandled promise rejection handler
- Automatic error reporting to server
- Comprehensive error metadata collection:
  - Error message and stack trace
  - URL, user agent, timestamp
  - Filename, line number, column number (for window errors)

Client Error Reporting Endpoint (app/api/client-error/route.ts):
- Server-side endpoint to receive client errors
- Integrated with withLogging() for automatic server logging
- Accepts error reports from both ErrorBoundary and global handlers
- Returns acknowledgement to client

Error Boundary Integration (components/providers/Providers.tsx):
- Wrapped entire app in ErrorBoundary
- Initialized global error handlers on mount
- Catches React errors, window errors, and unhandled rejections

Error Reporting Features:
- Duplicate error tracking prevention
- Async error reporting (non-blocking)
- Graceful degradation (fails silently if reporting fails)
- Development vs production error display
- Structured error metadata for debugging

All errors now:
- Log to browser console via client logger
- Report to server for centralized logging
- Display user-friendly error UI
- Include full context for debugging
- Work across React, window, and promise contexts

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-23 21:00:37 +01:00
parent 95acf4542b
commit 6985032006
4 changed files with 318 additions and 7 deletions

View File

@@ -1,10 +1,12 @@
'use client';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { useState, ReactNode } from 'react';
import { useState, ReactNode, useEffect } from 'react';
import { Toaster } from 'sonner';
import { ThemeProvider } from './ThemeProvider';
import { ErrorBoundary } from './ErrorBoundary';
import { clientLogger } from '@/lib/utils/client-logger';
import { initGlobalErrorHandlers } from '@/lib/utils/global-error-handler';
export function Providers({ children }: { children: ReactNode }) {
const [queryClient] = useState(
@@ -28,12 +30,19 @@ export function Providers({ children }: { children: ReactNode }) {
})
);
// Initialize global error handlers once
useEffect(() => {
initGlobalErrorHandlers();
}, []);
return (
<ThemeProvider>
<QueryClientProvider client={queryClient}>
{children}
<Toaster position="top-right" richColors closeButton />
</QueryClientProvider>
</ThemeProvider>
<ErrorBoundary>
<ThemeProvider>
<QueryClientProvider client={queryClient}>
{children}
<Toaster position="top-right" richColors closeButton />
</QueryClientProvider>
</ThemeProvider>
</ErrorBoundary>
);
}