-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathErrorHandler.jsx
More file actions
45 lines (39 loc) · 1.15 KB
/
ErrorHandler.jsx
File metadata and controls
45 lines (39 loc) · 1.15 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
import React from 'react';
import * as Sentry from '@sentry/react';
import PageNotFound from './pages/PageNotFound';
// eslint-disable-next-line import/prefer-default-export
export class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
// Provide a more descriptive Sentry breadcrumb
Sentry.addBreadcrumb({
category: 'ErrorBoundary',
message: `Render crash detected: ${error?.message}`,
level: 'error'
});
return { hasError: true };
}
componentDidCatch(error, errorInfo) {
// Send exception to sentry
Sentry.withScope((scope) => {
scope.setTag('Caught-at', 'Error Boundary');
scope.setExtras(errorInfo);
Sentry.captureException(error);
});
}
componentDidUpdate(prevProps) {
// Allow ErrorBoundary to recover when children change
if (prevProps.children !== this.props.children && this.state.hasError) {
this.setState({ hasError: false });
}
}
render() {
if (this.state.hasError) {
return <PageNotFound />;
}
return this.props.children;
}
}