43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
import React from "react";
|
|
|
|
import {trackEvent} from "@/Utils";
|
|
|
|
interface ErrorBoundaryState {
|
|
hasError: boolean;
|
|
errorMessage?: string;
|
|
}
|
|
|
|
interface ErrorBoundaryProps {
|
|
children: React.ReactNode;
|
|
}
|
|
|
|
export default class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
constructor(props: ErrorBoundaryProps) {
|
|
super(props);
|
|
this.state = { hasError: false };
|
|
}
|
|
|
|
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
|
|
return { hasError: true, errorMessage: error.message };
|
|
}
|
|
|
|
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
|
|
console.error("Caught an error:", error, errorInfo);
|
|
trackEvent("error", { error: error.message, errorInfo: JSON.stringify(errorInfo) });
|
|
}
|
|
|
|
render() {
|
|
if (this.state.hasError) {
|
|
// Render any custom fallback UI with the error message
|
|
return (
|
|
<div className="p-2">
|
|
<h1>Something went wrong.</h1>
|
|
<p>Error: {this.state.errorMessage}</p>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return this.props.children;
|
|
}
|
|
}
|