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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x | import React, { Component, ReactNode } from 'react';
import { logger } from './Logger';
interface Props {
children: ReactNode;
fallback?: ReactNode;
componentName?: string;
}
interface State {
hasError: boolean;
error?: Error;
errorInfo?: React.ErrorInfo;
}
/**
* Error boundary component with integrated logging
* Captures component errors and logs them with user context
*/
export class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error: Error): State {
return {
hasError: true,
error,
};
}
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
const componentName = this.props.componentName || 'Unknown Component';
// Log the error with full context
logger.logComponentError(componentName, error, {
metadata: {
componentStack: errorInfo.componentStack,
errorBoundary: true,
timestamp: new Date().toISOString(),
}
});
// Update state with error info
this.setState({
error,
errorInfo,
});
}
render() {
if (this.state.hasError) {
// Custom fallback UI
if (this.props.fallback) {
return this.props.fallback;
}
// Default fallback UI
return (
<div className="min-h-screen flex items-center justify-center bg-gray-50">
<div className="max-w-md w-full bg-white shadow-lg rounded-lg p-6">
<div className="flex items-center">
<div className="flex-shrink-0">
<svg
className="h-8 w-8 text-red-400"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z"
/>
</svg>
</div>
<div className="ml-3">
<h3 className="text-sm font-medium text-gray-800">
Something went wrong
</h3>
<div className="mt-2 text-sm text-gray-500">
<p>
We've encountered an unexpected error. The issue has been logged and our team will investigate.
</p>
</div>
<div className="mt-4">
<button
type="button"
className="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-red-700 bg-red-100 hover:bg-red-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500"
onClick={() => {
// Log the reload action
logger.logUserAction('error_boundary_reload', {
component: this.props.componentName || 'ErrorBoundary',
metadata: {
errorMessage: this.state.error?.message,
}
});
window.location.reload();
}}
>
Reload page
</button>
</div>
</div>
</div>
{/* Show error details in development */}
{import.meta.env.MODE === 'development' && this.state.error && (
<div className="mt-4 p-3 bg-gray-100 rounded-md">
<h4 className="text-xs font-medium text-gray-700 mb-2">
Development Error Details:
</h4>
<pre className="text-xs text-gray-600 whitespace-pre-wrap">
{this.state.error.message}
{this.state.error.stack}
</pre>
</div>
)}
</div>
</div>
);
}
return this.props.children;
}
}
/**
* HOC to wrap components with error boundary and logging
*/
export const withErrorBoundary = <P extends object>(
Component: React.ComponentType<P>,
componentName?: string,
fallback?: ReactNode
) => {
const WrappedComponent = (props: P) => (
<ErrorBoundary
componentName={componentName || Component.displayName || Component.name}
fallback={fallback}
>
<Component {...props} />
</ErrorBoundary>
);
WrappedComponent.displayName = `withErrorBoundary(${Component.displayName || Component.name})`;
return WrappedComponent;
};
|