-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathErrorBoundary.tsx
More file actions
executable file
·43 lines (36 loc) · 1.23 KB
/
ErrorBoundary.tsx
File metadata and controls
executable file
·43 lines (36 loc) · 1.23 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
import React, { Component, ReactNode } from "react";
import ProblemWithService from "../ProblemWithService/ProblemWithService";
import { ErrorBoundaryProps, ErrorBoundaryState } from "./ErrorBoundary.types";
class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
constructor(props: ErrorBoundaryProps) {
super(props);
this.state = {
hasError: false,
message: props.message || "",
applicationName: props.applicationName || "",
};
}
static getDerivedStateFromError(): Partial<ErrorBoundaryState> {
// Update state to render fallback UI on error
return { hasError: true };
}
componentDidCatch(error: Error, info: React.ErrorInfo): void {
console.error("Error caught by ErrorBoundary:", error, info);
// Optionally log error details to an external service here
this.setState({ message: error.message });
}
render(): ReactNode {
const { hasError, message, applicationName } = this.state;
if (hasError) {
return (
<ProblemWithService
message={message}
applicationName={applicationName}
basePageName={this.props.basePageName}
/>
);
}
return this.props.children;
}
}
export default ErrorBoundary;