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 | 27x 26x 13x 7x 7x 7x 7x 26x 1x 26x 47x 20x 3x 17x 27x 3x 2x 4x 2x 2x 3x 3x 2x 2x 1x 2x 2x 1x 2x | import type { ReactNode, ErrorInfo, ComponentType } from 'react';
import { Component, useState, useCallback, useEffect } from 'react';
import { Button } from '@/components/button';
import { Card } from '@/components/card';
interface Props {
children: ReactNode;
fallback?: ReactNode;
onError?: (error: Error, errorInfo: ErrorInfo) => void;
}
interface State {
hasError: boolean;
error: Error | null;
errorInfo: ErrorInfo | null;
}
export class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
hasError: false,
error: null,
errorInfo: null,
};
}
static getDerivedStateFromError(error: Error): Partial<State> {
return {
hasError: true,
error,
};
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
this.setState({
error,
errorInfo,
});
// Log error to monitoring service
console.error('Error caught by boundary:', error, errorInfo);
// Call optional error handler
this.props.onError?.(error, errorInfo);
// In production, send to error tracking service
Iif (process.env.NODE_ENV === 'production') {
// Example: Sentry.captureException(error, { contexts: { react: { componentStack: errorInfo.componentStack } } });
}
}
handleRetry = () => {
this.setState({
hasError: false,
error: null,
errorInfo: null,
});
};
handleReload = () => {
window.location.reload();
};
render() {
if (this.state.hasError) {
// Custom fallback UI
if (this.props.fallback) {
return this.props.fallback;
}
// Default error UI
return (
<div className="min-h-screen flex items-center justify-center p-4 bg-background">
<Card className="w-full max-w-md p-6 text-center">
<div className="mb-4">
<h2 className="text-2xl font-bold text-destructive mb-2">
Oops! Something went wrong
</h2>
<p className="text-muted-foreground">
We're sorry, but something unexpected happened. Please try
again.
</p>
</div>
<div className="flex flex-col sm:flex-row gap-2 justify-center">
<Button onClick={this.handleRetry} variant="outline">
Try Again
</Button>
<Button onClick={this.handleReload}>Reload Page</Button>
</div>
{process.env.NODE_ENV === 'development' && this.state.error && (
<details className="mt-6 text-left">
<summary className="cursor-pointer font-semibold text-sm mb-2">
Error Details (Development Only)
</summary>
<div className="bg-muted p-3 rounded text-xs font-mono overflow-auto max-h-40">
<pre>{this.state.error.toString()}</pre>
{this.state.errorInfo && (
<pre className="mt-2 text-muted-foreground">
{this.state.errorInfo.componentStack}
</pre>
)}
</div>
</details>
)}
</Card>
</div>
);
}
return this.props.children;
}
}
// Higher-order component for easy wrapping
export const withErrorBoundary = <P extends object>(
Component: ComponentType<P>,
errorBoundaryProps?: Omit<Props, 'children'>
) => {
const WrappedComponent = (props: P) => (
<ErrorBoundary {...errorBoundaryProps}>
<Component {...props} />
</ErrorBoundary>
);
WrappedComponent.displayName = `withErrorBoundary(${Component.displayName || Component.name})`;
return WrappedComponent;
};
// Hook for handling async errors
export const useErrorHandler = () => {
const [error, setError] = useState<Error | null>(null);
const resetError = useCallback(() => {
setError(null);
}, []);
const captureError = useCallback((error: Error) => {
setError(error);
}, []);
useEffect(() => {
if (error) {
throw error;
}
}, [error]);
return { captureError, resetError };
};
|