Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 23x 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 52x 52x 52x 1x 1x 47x 47x 1x 1x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 22x 22x 23x 23x 23x 1x 1x 23x 1x 1x 1x 1x 1x 1x 1x 1x 120x 120x 120x 120x 51x 51x 69x 69x 120x 6x 3x 3x 3x 3x 63x 63x 63x 120x 3x 120x 12x 120x 15x 120x 9x 120x 24x 24x 24x 24x 24x 24x 24x 24x 120x 120x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 8x 8x 8x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 4x 4x 4x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 1x 1x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x | 'use client';
import React, { Component, ReactNode, useState } from 'react';
import { clientLogger } from '@/lib/logging/clientLogger';
import { Icon } from '@/components/ui/icons';
import { Button } from '@/components/ui';
/**
* Error Boundary Variants
*/
export type ErrorBoundaryVariant = 'default' | 'admin' | 'checkout' | 'shop' | 'minimal';
/**
* Unified Error Boundary Props
*/
export interface ErrorBoundaryProps {
children: ReactNode;
/** Custom fallback UI - can be a ReactNode or a function receiving the error */
fallback?: ReactNode | ((error: Error) => ReactNode);
/** Error boundary variant for different contexts */
variant?: ErrorBoundaryVariant;
/** Custom error handler callback */
onError?: (error: Error, errorInfo: React.ErrorInfo) => void;
/** Logging context identifier */
context?: string;
/** Whether to report errors to backend monitoring */
reportToBackend?: boolean;
}
interface ErrorBoundaryState {
hasError: boolean;
error: Error | null;
errorId: string | null;
}
/**
* Generate a unique error ID for tracking
*/
function generateErrorId(): string {
return `err_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
/**
* Report error to backend monitoring
*/
async function reportError(
error: Error,
errorInfo: React.ErrorInfo,
errorId: string,
context?: string
): Promise<void> {
try {
await fetch('/api/monitoring/errors', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
message: error.message,
stack: error.stack,
componentStack: errorInfo.componentStack,
url: typeof window !== 'undefined' ? window.location.href : '',
userAgent: typeof navigator !== 'undefined' ? navigator.userAgent : '',
timestamp: new Date().toISOString(),
errorId,
context,
}),
});
} catch {
// Silently fail - don't throw during error handling
}
}
/**
* Unified Error Boundary Component
*
* Catches React errors and displays appropriate fallback UI based on context.
* Supports multiple variants for different use cases (admin, checkout, shop, etc.)
*
* @example
* ```tsx
* // Basic usage
* <ErrorBoundary>
* <MyComponent />
* </ErrorBoundary>
*
* // With variant
* <ErrorBoundary variant="admin" context="USER_MANAGEMENT">
* <AdminPanel />
* </ErrorBoundary>
*
* // With custom fallback
* <ErrorBoundary fallback={<CustomErrorUI />}>
* <MyComponent />
* </ErrorBoundary>
* ```
*/
export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
constructor(props: ErrorBoundaryProps) {
super(props);
this.state = { hasError: false, error: null, errorId: null };
}
static getDerivedStateFromError(error: Error): Partial<ErrorBoundaryState> {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void {
const { context, onError, reportToBackend = true } = this.props;
const errorId = generateErrorId();
this.setState({ errorId });
// Log error with structured logging
clientLogger.error('React component error caught', error, {
context: context || 'ERROR_BOUNDARY',
stack: error.stack,
componentStack: errorInfo.componentStack,
errorId,
});
// Report to backend monitoring
if (reportToBackend) {
reportError(error, errorInfo, errorId, context);
}
// Call custom error handler if provided
if (onError) {
onError(error, errorInfo);
}
}
handleReset = (): void => {
this.setState({ hasError: false, error: null, errorId: null });
};
handleRefresh = (): void => {
window.location.reload();
};
render(): ReactNode {
const { hasError, error, errorId } = this.state;
const { children, fallback, variant = 'default' } = this.props;
if (!hasError) {
return children;
}
// Custom fallback (function or ReactNode)
if (fallback) {
if (typeof fallback === 'function') {
return fallback(error!);
}
return fallback;
}
// Variant-specific fallback UI
switch (variant) {
case 'minimal':
return <MinimalErrorUI error={error} />;
case 'admin':
return <AdminErrorUI error={error} errorId={errorId} onRefresh={this.handleRefresh} />;
case 'checkout':
return <CheckoutErrorUI onRefresh={this.handleRefresh} />;
case 'shop':
return <ShopErrorUI onRefresh={this.handleRefresh} />;
default:
return (
<DefaultErrorUI
error={error}
errorId={errorId}
onReset={this.handleReset}
onRefresh={this.handleRefresh}
/>
);
}
}
}
// ============================================================================
// Variant-specific Error UI Components
// ============================================================================
interface ErrorUIProps {
error?: Error | null;
errorId?: string | null;
onReset?: () => void;
onRefresh?: () => void;
}
/**
* Minimal error display - just shows error message
*/
function MinimalErrorUI({ error }: ErrorUIProps) {
return (
<div className="bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 text-red-700 dark:text-red-400 px-4 py-3 rounded">
<p className="font-semibold">Something went wrong</p>
<p className="text-sm">{error?.message}</p>
</div>
);
}
/**
* Default error UI with reset and refresh options
*/
function DefaultErrorUI({ errorId, onReset, onRefresh }: ErrorUIProps) {
return (
<div className="min-h-[400px] flex items-center justify-center p-8">
<div className="max-w-md w-full bg-white dark:bg-gray-800 rounded-lg shadow-lg p-8 text-center">
<div className="mb-4">
<Icon name="alert-triangle" size={48} className="mx-auto text-red-500" />
</div>
<h3 className="text-lg font-semibold text-gray-900 dark:text-white mb-2">
Something went wrong
</h3>
<p className="text-sm text-gray-600 dark:text-gray-400 mb-2">
We encountered an error while loading this content. Please try again.
</p>
{errorId && (
<p className="text-xs text-gray-400 dark:text-gray-500 mb-6">
Error ID: {errorId}
</p>
)}
<div className="flex gap-4 justify-center">
<Button onClick={onRefresh} variant="primary" size="md">
Refresh Page
</Button>
<Button onClick={onReset} variant="ghost" size="md">
Try Again
</Button>
</div>
</div>
</div>
);
}
/**
* Admin-specific error UI with technical details toggle
*/
function AdminErrorUI({ error, errorId, onRefresh }: ErrorUIProps) {
const [showDetails, setShowDetails] = useState(false);
return (
<div className="min-h-[500px] flex items-center justify-center p-8">
<div className="max-w-2xl w-full bg-white dark:bg-gray-800 rounded-lg shadow-lg p-8">
<div className="flex items-start mb-6">
<div className="flex-shrink-0">
<Icon name="x-circle" size={48} className="text-red-500" />
</div>
<div className="ml-4 flex-1">
<h3 className="text-xl font-bold text-gray-900 dark:text-white mb-2">
Admin Panel Error
</h3>
<p className="text-gray-600 dark:text-gray-400 mb-2">
An error occurred while loading this admin component.
</p>
{errorId && (
<p className="text-xs text-gray-400 dark:text-gray-500 mb-4">
Error ID: {errorId}
</p>
)}
<div className="space-x-3">
<Button onClick={onRefresh} variant="primary" size="md">
Reload Page
</Button>
<Button
onClick={() => setShowDetails(!showDetails)}
variant="ghost"
size="md"
>
{showDetails ? 'Hide' : 'Show'} Technical Details
</Button>
</div>
{showDetails && error && (
<div className="mt-6 bg-gray-100 dark:bg-gray-900 rounded-md p-4 overflow-auto">
<h4 className="font-semibold text-gray-900 dark:text-white mb-2">
Error Details:
</h4>
<pre className="text-xs text-red-600 dark:text-red-400 whitespace-pre-wrap">
{error.message}
</pre>
{error.stack && (
<>
<h4 className="font-semibold text-gray-900 dark:text-white mt-4 mb-2">
Stack Trace:
</h4>
<pre className="text-xs text-gray-700 dark:text-gray-300 whitespace-pre-wrap">
{error.stack}
</pre>
</>
)}
</div>
)}
</div>
</div>
</div>
</div>
);
}
/**
* Checkout-specific error UI with payment safety messaging
*/
function CheckoutErrorUI({ onRefresh }: ErrorUIProps) {
return (
<div className="min-h-[600px] flex items-center justify-center p-8 bg-gray-50 dark:bg-gray-900">
<div className="max-w-md w-full bg-white dark:bg-gray-800 rounded-lg shadow-lg p-8">
<div className="text-center mb-6">
<Icon name="alert-triangle" size={64} className="mx-auto text-orange-500 mb-4" />
<h3 className="text-xl font-bold text-gray-900 dark:text-white mb-2">
Checkout Error
</h3>
<p className="text-gray-600 dark:text-gray-400 mb-4">
We encountered an issue with the checkout process. Your payment has not been processed.
</p>
</div>
<div className="bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-md p-4 mb-6">
<p className="text-sm text-blue-800 dark:text-blue-300">
<strong>Don't worry:</strong> No charges have been made to your payment method.
</p>
</div>
<div className="space-y-3">
<Button onClick={onRefresh} variant="primary" size="md" fullWidth>
Try Again
</Button>
<a
href="/cart"
className="block w-full text-center bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-200 px-4 py-3 rounded-md hover:bg-gray-300 dark:hover:bg-gray-600 transition-colors font-medium"
>
Return to Cart
</a>
</div>
</div>
</div>
);
}
/**
* Shop-specific error UI - minimal disruption for browsing
*/
function ShopErrorUI({ onRefresh }: ErrorUIProps) {
return (
<div className="min-h-[300px] flex items-center justify-center p-8">
<div className="max-w-md w-full text-center">
<Icon name="package" size={48} className="mx-auto text-gray-400 dark:text-gray-500 mb-4" />
<h3 className="text-lg font-semibold text-gray-900 dark:text-white mb-2">
Unable to Load Products
</h3>
<p className="text-sm text-gray-600 dark:text-gray-400 mb-6">
We're having trouble loading this content. Please refresh or try again later.
</p>
<Button onClick={onRefresh} variant="primary" size="md">
Refresh Page
</Button>
</div>
</div>
);
}
// ============================================================================
// Convenience Wrapper Components
// ============================================================================
/**
* Admin Error Boundary - wrapper with admin variant pre-configured
*/
export function AdminErrorBoundary({ children }: { children: ReactNode }) {
return (
<ErrorBoundary variant="admin" context="ADMIN">
{children}
</ErrorBoundary>
);
}
/**
* Checkout Error Boundary - wrapper with checkout variant pre-configured
*/
export function CheckoutErrorBoundary({ children }: { children: ReactNode }) {
return (
<ErrorBoundary variant="checkout" context="CHECKOUT">
{children}
</ErrorBoundary>
);
}
/**
* Shop Error Boundary - wrapper with shop variant pre-configured
*/
export function ShopErrorBoundary({ children }: { children: ReactNode }) {
return (
<ErrorBoundary variant="shop" context="SHOP">
{children}
</ErrorBoundary>
);
}
// Full Page Error component for root-level catches
export { FullPageError, type FullPageErrorProps } from './FullPageError';
export default ErrorBoundary;
|