Repository navigation
Expand file tree
/
Copy pathErrorBoundary.tsx
More file actions
59 lines (49 loc) · 1.64 KB
/
Copy pathErrorBoundary.tsx
File metadata and controls
59 lines (49 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { ErrorInfo, ReactElement, ReactNode } from 'react';
import { Component } from 'react';
import styles from './ErrorBoundary.module.scss';
interface ErrorBoundaryProps {
children: ReactNode;
fallback?: ReactElement;
}
interface ErrorBoundaryState {
hasError: boolean;
error: Error | null;
}
export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
constructor(props: ErrorBoundaryProps) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: ErrorInfo): void {
// Log error for debugging purposes
console.error('ErrorBoundary caught an error:', error, errorInfo);
}
render(): ReactNode {
if (this.state.hasError) {
if (this.props.fallback) {
return this.props.fallback;
}
return (
<div className={styles.ErrorContainer}>
<div className={styles.ErrorContent}>
<h3 className={styles.ErrorTitle}>Failed to load viewer</h3>
<p className={styles.ErrorMessage}>
There was an error loading this content. Please try selecting a different source or
refreshing the page.
</p>
{this.state.error && (
<details className={styles.ErrorDetails}>
<summary>Error details</summary>
<pre className={styles.ErrorStack}>{this.state.error.toString()}</pre>
</details>
)}
</div>
</div>
);
}
return this.props.children;
}
}