import {type ReactNode, Component} from 'react'; import InstrumentationModule from '../../native/InstrumentationModule'; import {createCrashReport} from '../functions/createCrashReport'; interface ErrorBoundaryProps { children: ReactNode; fallbackUI: ReactNode; } interface ErrorBoundaryState { hasError: boolean; } /** * Custom React Error Boundary component that both captures and redirects events to the AppDynamics controller. * * @see https://reactjs.org/docs/error-boundaries.html */ /* * CDM-9426 TODO: convert to functional component when possible. Current docs recommend * class. */ class ErrorBoundary extends Component { constructor(props: ErrorBoundaryProps) { super(props); this.state = {hasError: false}; } static getDerivedStateFromError() { // Update state so the next render will show the fallback UI. return {hasError: true}; } async componentDidCatch(error: Error) { const hybridExceptionData = createCrashReport(error); const serialized = JSON.stringify(hybridExceptionData); await InstrumentationModule.createCrashReport(serialized); } render() { const {fallbackUI, children} = this.props; const {hasError} = this.state; return hasError ? fallbackUI : children; } } export default ErrorBoundary;