UNPKG

@memberjunction/react-runtime

Version:

Platform-agnostic React component runtime for MemberJunction. Provides core compilation, registry, and execution capabilities for React components in any JavaScript environment.

140 lines 5.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createErrorLogger = exports.formatComponentError = exports.withErrorBoundary = exports.createErrorBoundary = void 0; function createErrorBoundary(React, options = {}) { const { onError, fallback, logErrors = true, recovery = 'none' } = options; return class ErrorBoundary extends React.Component { constructor(props) { super(props); this.handleRetry = () => { this.setState((prevState) => ({ hasError: false, error: null, errorInfo: null, retryCount: prevState.retryCount + 1 })); }; this.handleReset = () => { this.setState({ hasError: false, error: null, errorInfo: null, retryCount: 0 }); }; this.state = { hasError: false, error: null, errorInfo: null, retryCount: 0 }; } static getDerivedStateFromError(error) { return { hasError: true, error }; } componentDidCatch(error, errorInfo) { if (logErrors) { console.error('React Error Boundary caught error:', error); console.error('Error Info:', errorInfo); } if (onError) { try { onError(error, errorInfo); } catch (handlerError) { console.error('Error in custom error handler:', handlerError); } } this.setState({ errorInfo }); } render() { if (this.state.hasError) { if (fallback) { if (typeof fallback === 'function') { return fallback({ error: this.state.error, errorInfo: this.state.errorInfo, retry: this.handleRetry, reset: this.handleReset, retryCount: this.state.retryCount }); } return fallback; } const showRetry = recovery === 'retry' && this.state.retryCount < 3; const showReset = recovery === 'reset'; return React.createElement('div', { style: { padding: '20px', backgroundColor: '#f8f8f8', border: '1px solid #ddd', borderRadius: '4px', margin: '10px' } }, React.createElement('h2', { style: { color: '#d32f2f' } }, 'Component Error'), React.createElement('p', { style: { color: '#666' } }, 'An error occurred while rendering this component.'), this.state.error && React.createElement('details', { style: { marginTop: '10px' } }, React.createElement('summary', { style: { cursor: 'pointer', color: '#333' } }, 'Error Details'), React.createElement('pre', { style: { backgroundColor: '#f0f0f0', padding: '10px', marginTop: '10px', overflow: 'auto', fontSize: '12px' } }, this.state.error.toString(), '\n\n', this.state.error.stack)), (showRetry || showReset) && React.createElement('div', { style: { marginTop: '10px' } }, showRetry && React.createElement('button', { onClick: this.handleRetry, style: { padding: '8px 16px', marginRight: '10px', backgroundColor: '#1976d2', color: 'white', border: 'none', borderRadius: '4px', cursor: 'pointer' } }, `Retry (${3 - this.state.retryCount} attempts left)`), showReset && React.createElement('button', { onClick: this.handleReset, style: { padding: '8px 16px', backgroundColor: '#757575', color: 'white', border: 'none', borderRadius: '4px', cursor: 'pointer' } }, 'Reset Component'))); } return this.props.children; } }; } exports.createErrorBoundary = createErrorBoundary; function withErrorBoundary(React, Component, options = {}) { const ErrorBoundaryComponent = createErrorBoundary(React, options); return (props) => { return React.createElement(ErrorBoundaryComponent, null, React.createElement(Component, props)); }; } exports.withErrorBoundary = withErrorBoundary; function formatComponentError(error, componentName, phase) { return { message: error.message || 'Unknown error', stack: error.stack, componentName, phase, details: { name: error.name, timestamp: new Date().toISOString() } }; } exports.formatComponentError = formatComponentError; function createErrorLogger(componentName) { return (error, errorInfo) => { console.group(`🚨 React Component Error: ${componentName}`); console.error('Error:', error); console.error('Component Stack:', errorInfo.componentStack); console.error('Props:', errorInfo.props); console.groupEnd(); }; } exports.createErrorLogger = createErrorLogger; //# sourceMappingURL=error-boundary.js.map