/
errorboundary.tsx
124 lines (105 loc) · 3.66 KB
/
errorboundary.tsx
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import * as Sentry from '@sentry/browser';
import * as hoistNonReactStatic from 'hoist-non-react-statics';
import * as React from 'react';
export const FALLBACK_ERR_MESSAGE = 'No fallback component has been set';
export const UNKNOWN_COMPONENT = 'unknown';
export type ErrorBoundaryProps = {
showDialog?: boolean;
dialogOptions?: Sentry.ReportDialogOptions;
fallback?: React.ReactNode;
renderKey?: string | number;
fallbackRender?(fallback: {
error: Error | null;
componentStack: string | null;
resetError(): void;
}): React.ReactNode;
onError?(error: Error, componentStack: string): void;
onMount?(): void;
onReset?(error: Error | null, componentStack: string | null): void;
onUnmount?(error: Error | null, componentStack: string | null): void;
};
type ErrorBoundaryState = {
componentStack: string | null;
error: Error | null;
};
const INITIAL_STATE = {
componentStack: null,
error: null,
};
class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
public state: ErrorBoundaryState = INITIAL_STATE;
public componentDidCatch(error: Error, { componentStack }: React.ErrorInfo): void {
Sentry.captureException(error, { contexts: { componentStack } });
const { onError, showDialog, dialogOptions } = this.props;
if (onError) {
onError(error, componentStack);
}
if (showDialog) {
Sentry.showReportDialog(dialogOptions);
}
this.setState({ error, componentStack });
}
public componentDidMount(): void {
const { onMount } = this.props;
if (onMount) {
onMount();
}
}
// If render key changes and there is an error, the component is reset.
// This provides an easy way for users to reset their error boundary.
public componentDidUpdate(prevProps: ErrorBoundaryProps): void {
const { error } = this.state;
const { renderKey, onReset } = this.props;
if (error !== null && !Object.is(renderKey, prevProps.renderKey)) {
if (onReset) {
onReset(this.state.error, this.state.componentStack);
}
this.setState(INITIAL_STATE);
}
}
public componentWillUnmount(): void {
const { error, componentStack } = this.state;
const { onUnmount } = this.props;
if (onUnmount) {
onUnmount(error, componentStack);
}
}
public resetErrorBoundary = () => {
const { onReset } = this.props;
if (onReset) {
onReset(this.state.error, this.state.componentStack);
}
this.setState(INITIAL_STATE);
};
public render(): React.ReactNode {
const { fallback, fallbackRender } = this.props;
const { error, componentStack } = this.state;
if (error) {
if (typeof fallbackRender === 'function') {
return fallbackRender({ error, componentStack, resetError: this.resetErrorBoundary });
}
if (React.isValidElement(fallback)) {
return fallback;
}
throw new Error(FALLBACK_ERR_MESSAGE);
}
return this.props.children;
}
}
function withErrorBoundary<P extends object>(
WrappedComponent: React.ComponentType<P>,
errorBoundaryOptions: ErrorBoundaryProps,
): React.FC<P> {
const componentDisplayName = WrappedComponent.displayName || WrappedComponent.name || UNKNOWN_COMPONENT;
const Wrapped: React.FC<P> = (props: P) => (
<ErrorBoundary {...errorBoundaryOptions}>
<WrappedComponent {...props} />
</ErrorBoundary>
);
Wrapped.displayName = `boundary(${componentDisplayName})`;
// Copy over static methods from Wrapped component to Profiler HOC
// See: https://reactjs.org/docs/higher-order-components.html#static-methods-must-be-copied-over
hoistNonReactStatic(Wrapped, WrappedComponent);
return Wrapped;
}
export { ErrorBoundary, withErrorBoundary };