import React, { Component, ReactNode } from 'react'; interface Props { children: ReactNode; fallback?: ReactNode; onError?: (error: Error, errorInfo: React.ErrorInfo) => void; } interface State { hasError: boolean; error: Error | null; } /** * Error Boundary component to catch and handle React errors * Prevents entire app from crashing when a component throws * * @example * }> * * */ export class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false, error: null, }; } static getDerivedStateFromError(error: Error): State { return { hasError: true, error, }; } componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { // Log error to console in development console.error('ErrorBoundary caught an error:', error, errorInfo); // Call optional error handler this.props.onError?.(error, errorInfo); } handleReset = () => { this.setState({ hasError: false, error: null, }); }; render() { if (this.state.hasError) { // Render custom fallback or default error UI if (this.props.fallback) { return this.props.fallback; } return (
⚠️ Что-то пошло не так
{this.state.error?.message || 'Произошла неизвестная ошибка'}
); } return this.props.children; } } /** * Specific Error Boundary for Builder components */ export function BuilderErrorBoundary({ children }: { children: ReactNode }) { return (
⚠️ Ошибка в билдере
Не удалось загрузить компонент. Попробуйте перезагрузить страницу.
} onError={(error) => { // Could send to error tracking service here console.error('[Builder Error]:', error); }} > {children}
); } /** * Specific Error Boundary for Preview component */ export function PreviewErrorBoundary({ children }: { children: ReactNode }) { return (
⚠️ Ошибка превью
Не удалось отобразить превью экрана
} > {children}
); }