-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy patherror-boundary.js
More file actions
73 lines (60 loc) · 1.98 KB
/
error-boundary.js
File metadata and controls
73 lines (60 loc) · 1.98 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use client';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import invariant from 'tiny-invariant';
import { LEVEL_ERROR } from './constants';
import { Context, getRollbarFromContext } from './provider';
import * as utils from './utils';
const INITIAL_ERROR_STATE = { hasError: false, error: null };
export class ErrorBoundary extends Component {
static contextType = Context;
static propTypes = {
fallbackUI: PropTypes.elementType,
errorMessage: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
extra: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
level: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
callback: PropTypes.func,
children: PropTypes.node,
};
static defaultProps = {
level: LEVEL_ERROR,
};
constructor(props) {
super(props);
invariant(
utils.isValidLevel(props.level),
`${props.level} is not a valid level setting for Rollbar`,
);
this.state = { ...INITIAL_ERROR_STATE };
}
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
componentDidCatch(error, info) {
const { errorMessage, extra, level: targetLevel, callback } = this.props;
const custom = utils.value(extra, {}, error, info);
const data = { ...info, ...custom };
const level = utils.value(targetLevel, LEVEL_ERROR, error, info);
const rollbar = getRollbarFromContext(this.context);
if (!errorMessage) {
rollbar[level](error, data, callback);
} else {
let logMessage = utils.value(errorMessage, '', error, info);
rollbar[level](logMessage, error, data, callback);
}
}
resetError = () => {
this.setState(INITIAL_ERROR_STATE);
};
render() {
const { hasError, error } = this.state;
const { fallbackUI: FallbackUI, children } = this.props;
if (!hasError) {
return children;
}
if (!FallbackUI) {
return null;
}
return <FallbackUI error={error} resetError={this.resetError} />;
}
}