-
Notifications
You must be signed in to change notification settings - Fork 12
/
ErrorBoundary.jsx
63 lines (55 loc) · 1.69 KB
/
ErrorBoundary.jsx
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
60
61
62
63
/*eslint no-process-env: 0*/
import React from 'react';
import ErrorUI from './ErrorUI';
import { getCurrentUser, getEnv } from '../../common/utils';
import { Notifier } from '@airbrake/browser';
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = {
hasError: false,
error: null,
errorInfo: null
};
}
getNotifier = () => {
/*eslint no-undef: 0*/
this.ERRBIT_KEY = window.ERRBIT_KEY || process.env.ERRBIT_KEY
/*eslint no-undef: 0*/
this.ERRBIT_URL = window.ERRBIT_URL || process.env.ERRBIT_URL
if(this.ERRBIT_URL && this.ERRBIT_KEY)
return new Notifier({
projectId: 1,
projectKey: this.ERRBIT_KEY,
environment: getEnv(),
host: this.ERRBIT_URL
})
}
componentDidCatch(error, errorInfo) {
this.setState({error: error, errorInfo: errorInfo, hasError: Boolean(error)}, () => {
const notifier = this.getNotifier()
if(notifier) {
const user = getCurrentUser() || {};
notifier.notify({
error: this.state.error,
params: {info: this.state.errorInfo},
context: {
component: window.location.href, user: {id: user.id, email: user.email}
}
});
}
})
}
getErrorUIProps() {
const props = {header: 'Error', message: 'Something went wrong.'}
if(window.location.hash.match(/debug=true/))
return {...props, error: this.state.error, errorInfo: this.state.errorInfo}
return props
}
render() {
const props = this.getErrorUIProps()
const { hasError } = this.state;
return hasError ? <ErrorUI {...props} /> : this.props.children;
}
}
export default ErrorBoundary;