PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 1.0.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v1.0.0
2.7.0 2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.10.0 All 48 releases
thinkrank / src / metabox / components / ErrorBoundary.js

ErrorBoundary.js in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 1.0.0, at src/metabox/components/ErrorBoundary.js

158 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Error Boundary Component for ThinkRank Metabox
3 *
4 * Catches JavaScript errors in child components and displays
5 * a fallback UI instead of crashing the entire metabox.
6 *
7 * @package ThinkRank
8 * @since 1.0.0
9 */
10
11 import { Component } from '@wordpress/element';
12 import { __ } from '@wordpress/i18n';
13
14 /**
15 * Error Boundary Class Component
16 *
17 * React error boundaries must be class components to catch errors
18 * during rendering, in lifecycle methods, and in constructors.
19 */
20 class ErrorBoundary extends Component {
21 constructor(props) {
22 super(props);
23 this.state = {
24 hasError: false,
25 error: null,
26 errorInfo: null
27 };
28 }
29
30 /**
31 * Update state when an error is caught
32 *
33 * @param {Error} error The error that was thrown
34 * @return {Object} New state object
35 */
36 static getDerivedStateFromError(error) {
37 return {
38 hasError: true,
39 error: error
40 };
41 }
42
43 /**
44 * Log error details and notify parent component
45 *
46 * @param {Error} error The error that was thrown
47 * @param {Object} errorInfo Additional error information
48 */
49 componentDidCatch(error, errorInfo) {
50 this.setState({
51 error: error,
52 errorInfo: errorInfo
53 });
54
55 // Notify parent component if callback provided
56 if (this.props.onError) {
57 this.props.onError(error, errorInfo);
58 }
59
60 // In development, you might want to log to external service
61 // In production, avoid logging sensitive information
62 if (process.env.NODE_ENV === 'development') {
63 // Development logging only - removed in production builds
64 }
65 }
66
67 /**
68 * Reset error boundary state
69 */
70 resetErrorBoundary = () => {
71 this.setState({
72 hasError: false,
73 error: null,
74 errorInfo: null
75 });
76 }
77
78 /**
79 * Render error UI or children
80 */
81 render() {
82 if (this.state.hasError) {
83 // Custom error UI
84 return (
85 <div className="thinkrank-error-boundary">
86 <div className="notice notice-error">
87 <h4>{__('Something went wrong', 'thinkrank')}</h4>
88 <p>
89 {this.props.fallbackMessage ||
90 __('This component encountered an error and could not be displayed.', 'thinkrank')}
91 </p>
92 <div className="error-actions">
93 <button
94 type="button"
95 className="button button-secondary"
96 onClick={this.resetErrorBoundary}
97 >
98 {__('Try Again', 'thinkrank')}
99 </button>
100 <button
101 type="button"
102 className="button button-secondary"
103 onClick={() => window.location.reload()}
104 >
105 {__('Refresh Page', 'thinkrank')}
106 </button>
107 </div>
108 </div>
109
110 {/* Development error details */}
111 {process.env.NODE_ENV === 'development' && this.state.error && (
112 <details className="error-details" style={{ marginTop: '10px' }}>
113 <summary>{__('Error Details (Development Only)', 'thinkrank')}</summary>
114 <pre style={{
115 background: '#f1f1f1',
116 padding: '10px',
117 fontSize: '12px',
118 overflow: 'auto',
119 maxHeight: '200px'
120 }}>
121 {this.state.error.toString()}
122 {this.state.errorInfo && this.state.errorInfo.componentStack}
123 </pre>
124 </details>
125 )}
126 </div>
127 );
128 }
129
130 return this.props.children;
131 }
132 }
133
134 /**
135 * Higher-order component to wrap components with error boundary
136 *
137 * @param {React.Component} WrappedComponent Component to wrap
138 * @param {Object} options Error boundary options
139 * @return {React.Component} Component wrapped with error boundary
140 */
141 export const withErrorBoundary = (WrappedComponent, options = {}) => {
142 const WithErrorBoundaryComponent = (props) => (
143 <ErrorBoundary
144 fallbackMessage={options.fallbackMessage}
145 onError={options.onError}
146 >
147 <WrappedComponent {...props} />
148 </ErrorBoundary>
149 );
150
151 WithErrorBoundaryComponent.displayName =
152 `withErrorBoundary(${WrappedComponent.displayName || WrappedComponent.name})`;
153
154 return WithErrorBoundaryComponent;
155 };
156
157 export default ErrorBoundary;
158