import React from "react"; import { AlertTriangle, RefreshCw } from "lucide-react"; /** * ErrorBoundary - Prevents crashes from malformed AI responses or render errors * Used to wrap individual message components so one bad message doesn't crash the whole chat */ export default class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error) { return { hasError: true, error }; } componentDidCatch(error, errorInfo) { // Log error for debugging console.error("[ErrorBoundary] Caught error:", error, errorInfo); } handleRetry = () => { this.setState({ hasError: false, error: null }); }; render() { if (this.state.hasError) { // Minimal fallback UI if (this.props.minimal) { return (
Failed to display content
); } // Full fallback UI return (

Something went wrong

{this.props.fallbackMessage || "This content couldn't be displayed. Try refreshing or continue your conversation."}

); } return this.props.children; } } /** * Hook-friendly wrapper for functional components */ export function withErrorBoundary(Component, options = {}) { return function WrappedComponent(props) { return ( ); }; }