PluginProbe
Slider Block by Sliderberg – WordPress Slider & Carousel Plugin for Gutenberg / 1.2.3
Slider Block by Sliderberg – WordPress Slider & Carousel Plugin for Gutenberg v1.2.3
1.2.3 1.2.2 1.2.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.7 1.0.8 1.0.9
sliderberg / shared / src / components / ErrorBoundary.tsx

ErrorBoundary.tsx in Slider Block by Sliderberg – WordPress Slider & Carousel Plugin for Gutenberg 1.2.3, at shared/src/components/ErrorBoundary.tsx

120 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React, { Component, ErrorInfo, ReactNode } from 'react';
2 import { __ } from '@wordpress/i18n';
3 import { Button } from '@wordpress/components';
4
5 interface ErrorBoundaryProps {
6 children: ReactNode;
7 fallback?: ReactNode;
8 }
9
10 interface ErrorBoundaryState {
11 hasError: boolean;
12 error: Error | null;
13 errorInfo: ErrorInfo | null;
14 }
15
16 /**
17 * Error Boundary component for SliderBerg blocks
18 * Catches JavaScript errors in child components and displays a fallback UI
19 */
20 export class ErrorBoundary extends Component<
21 ErrorBoundaryProps,
22 ErrorBoundaryState
23 > {
24 constructor( props: ErrorBoundaryProps ) {
25 super( props );
26 this.state = {
27 hasError: false,
28 error: null,
29 errorInfo: null,
30 };
31 }
32
33 static getDerivedStateFromError(
34 error: Error
35 ): Partial< ErrorBoundaryState > {
36 // Update state so the next render will show the fallback UI
37 return { hasError: true, error };
38 }
39
40 componentDidCatch( error: Error, errorInfo: ErrorInfo ): void {
41 // Log error details for debugging
42 this.setState( {
43 error,
44 errorInfo,
45 } );
46
47 // Log to console in development
48 if ( process.env.NODE_ENV === 'development' ) {
49 // eslint-disable-next-line no-console
50 console.error(
51 'SliderBerg Error Boundary caught an error:',
52 error,
53 errorInfo
54 );
55 }
56 }
57
58 handleReset = (): void => {
59 this.setState( {
60 hasError: false,
61 error: null,
62 errorInfo: null,
63 } );
64 };
65
66 render(): ReactNode {
67 if ( this.state.hasError ) {
68 // Custom fallback UI provided
69 if ( this.props.fallback ) {
70 return this.props.fallback;
71 }
72
73 // Default fallback UI
74 return (
75 <div className="sliderberg-error-boundary">
76 <div className="sliderberg-error-content">
77 <h3>{ __( 'Something went wrong', 'sliderberg' ) }</h3>
78 <p>
79 { __(
80 'An error occurred while rendering this slider block.',
81 'sliderberg'
82 ) }
83 </p>
84 { process.env.NODE_ENV === 'development' &&
85 this.state.error && (
86 <details style={ { marginTop: '1rem' } }>
87 <summary>
88 { __( 'Error Details', 'sliderberg' ) }
89 </summary>
90 <pre
91 style={ {
92 padding: '1rem',
93 background: '#f5f5f5',
94 overflow: 'auto',
95 fontSize: '12px',
96 } }
97 >
98 { this.state.error.toString() }
99 { '\n\n' }
100 { this.state.errorInfo?.componentStack }
101 </pre>
102 </details>
103 ) }
104 <div style={ { marginTop: '1rem' } }>
105 <Button
106 variant="primary"
107 onClick={ this.handleReset }
108 >
109 { __( 'Try Again', 'sliderberg' ) }
110 </Button>
111 </div>
112 </div>
113 </div>
114 );
115 }
116
117 return this.props.children;
118 }
119 }
120