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 | 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x | import { Component, ErrorInfo, ReactNode } from "react";
import { toast } from "react-hot-toast";
import { logger } from "@common/utils/logger/Logger";
interface Props {
children: ReactNode;
fallback?: ReactNode;
onError?: (error: Error, errorInfo: ErrorInfo) => void;
}
interface State {
hasError: boolean;
error?: Error;
}
class ErrorBoundary extends Component<Props, State> {
public state: State = {
hasError: false,
};
public static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
// Log error with structured logging
logger.logComponentError("ErrorBoundary", error, {
metadata: {
componentStack: errorInfo.componentStack,
errorBoundary: true,
timestamp: new Date().toISOString(),
},
});
// Show error toast
toast.error(`Something went wrong: ${error.message}`);
// Call custom error handler if provided
if (this.props.onError) {
this.props.onError(error, errorInfo);
}
}
public render() {
if (this.state.hasError) {
// Render fallback UI if provided
if (this.props.fallback) {
return this.props.fallback;
}
// Default error UI
return (
<div className="p-6 max-w-md mx-auto mt-10 bg-white rounded-lg shadow-md">
<div className="text-center">
<div className="mx-auto flex items-center justify-center h-12 w-12 rounded-full bg-red-100">
<svg
className="h-6 w-6 text-red-600"
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-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
/>
</svg>
</div>
<h3 className="mt-3 text-lg font-medium text-gray-900">
Something went wrong
</h3>
<p className="mt-2 text-sm text-gray-500">
{this.state.error?.message || "An unexpected error occurred"}
</p>
<div className="mt-6">
<button
type="button"
onClick={() => {
// Log the reload action
logger.logUserAction("error_boundary_reload", {
component: "ErrorBoundary",
metadata: {
errorMessage: this.state.error?.message,
},
});
window.location.reload();
}}
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
>
Reload page
</button>
</div>
</div>
</div>
);
}
return this.props.children;
}
}
export default ErrorBoundary;
|