-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
errorboundary.tsx
206 lines (179 loc) · 7.18 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import type { ReportDialogOptions, Scope } from '@sentry/browser';
import { captureException, showReportDialog, withScope } from '@sentry/browser';
import { isError, logger } from '@sentry/utils';
import hoistNonReactStatics from 'hoist-non-react-statics';
import * as React from 'react';
export function isAtLeastReact17(version: string): boolean {
const major = version.match(/^([^.]+)/);
return major !== null && parseInt(major[0]) >= 17;
}
export const UNKNOWN_COMPONENT = 'unknown';
export type FallbackRender = (errorData: {
error: Error;
componentStack: string;
eventId: string;
resetError(): void;
}) => React.ReactElement;
export type ErrorBoundaryProps = {
children?: React.ReactNode | (() => React.ReactNode);
/** If a Sentry report dialog should be rendered on error */
showDialog?: boolean;
/**
* Options to be passed into the Sentry report dialog.
* No-op if {@link showDialog} is false.
*/
dialogOptions?: ReportDialogOptions;
/**
* A fallback component that gets rendered when the error boundary encounters an error.
*
* Can either provide a React Component, or a function that returns React Component as
* a valid fallback prop. If a function is provided, the function will be called with
* the error, the component stack, and an function that resets the error boundary on error.
*
*/
fallback?: React.ReactElement | FallbackRender;
/** Called when the error boundary encounters an error */
onError?(error: Error, componentStack: string, eventId: string): void;
/** Called on componentDidMount() */
onMount?(): void;
/** Called if resetError() is called from the fallback render props function */
onReset?(error: Error | null, componentStack: string | null, eventId: string | null): void;
/** Called on componentWillUnmount() */
onUnmount?(error: Error | null, componentStack: string | null, eventId: string | null): void;
/** Called before the error is captured by Sentry, allows for you to add tags or context using the scope */
beforeCapture?(scope: Scope, error: Error | null, componentStack: string | null): void;
};
type ErrorBoundaryState =
| {
componentStack: null;
error: null;
eventId: null;
}
| {
componentStack: React.ErrorInfo['componentStack'];
error: Error;
eventId: string;
};
const INITIAL_STATE = {
componentStack: null,
error: null,
eventId: null,
};
function setCause(error: Error & { cause?: Error }, cause: Error): void {
if (error.cause) {
return setCause(error.cause, cause);
}
error.cause = cause;
}
/**
* A ErrorBoundary component that logs errors to Sentry. Requires React >= 16.
* NOTE: If you are a Sentry user, and you are seeing this stack frame, it means the
* Sentry React SDK ErrorBoundary caught an error invoking your application code. This
* is expected behavior and NOT indicative of a bug with the Sentry React SDK.
*/
class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
public state: ErrorBoundaryState = INITIAL_STATE;
public componentDidCatch(error: Error & { cause?: Error }, { componentStack }: React.ErrorInfo): void {
const { beforeCapture, onError, showDialog, dialogOptions } = this.props;
withScope(scope => {
// If on React version >= 17, create stack trace from componentStack param and links
// to to the original error using `error.cause` otherwise relies on error param for stacktrace.
// Linking errors requires the `LinkedErrors` integration be enabled.
// See: https://reactjs.org/blog/2020/08/10/react-v17-rc.html#native-component-stacks
//
// Although `componentDidCatch` is typed to accept an `Error` object, it can also be invoked
// with non-error objects. This is why we need to check if the error is an error-like object.
// See: https://github.com/getsentry/sentry-javascript/issues/6167
if (isAtLeastReact17(React.version) && isError(error)) {
const errorBoundaryError = new Error(error.message);
errorBoundaryError.name = `React ErrorBoundary ${errorBoundaryError.name}`;
errorBoundaryError.stack = componentStack;
// Using the `LinkedErrors` integration to link the errors together.
setCause(error, errorBoundaryError);
}
if (beforeCapture) {
beforeCapture(scope, error, componentStack);
}
const eventId = captureException(error, { contexts: { react: { componentStack } } });
if (onError) {
onError(error, componentStack, eventId);
}
if (showDialog) {
showReportDialog({ ...dialogOptions, eventId });
}
// componentDidCatch is used over getDerivedStateFromError
// so that componentStack is accessible through state.
this.setState({ error, componentStack, eventId });
});
}
public componentDidMount(): void {
const { onMount } = this.props;
if (onMount) {
onMount();
}
}
public componentWillUnmount(): void {
const { error, componentStack, eventId } = this.state;
const { onUnmount } = this.props;
if (onUnmount) {
onUnmount(error, componentStack, eventId);
}
}
public resetErrorBoundary: () => void = () => {
const { onReset } = this.props;
const { error, componentStack, eventId } = this.state;
if (onReset) {
onReset(error, componentStack, eventId);
}
this.setState(INITIAL_STATE);
};
public render(): React.ReactNode {
const { fallback, children } = this.props;
const state = this.state;
if (state.error) {
let element: React.ReactElement | undefined = undefined;
if (typeof fallback === 'function') {
element = fallback({
error: state.error,
componentStack: state.componentStack,
resetError: this.resetErrorBoundary,
eventId: state.eventId,
});
} else {
element = fallback;
}
if (React.isValidElement(element)) {
return element;
}
if (fallback) {
__DEBUG_BUILD__ && logger.warn('fallback did not produce a valid ReactElement');
}
// Fail gracefully if no fallback provided or is not valid
return null;
}
if (typeof children === 'function') {
return (children as () => React.ReactNode)();
}
return children;
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function withErrorBoundary<P extends Record<string, any>>(
WrappedComponent: React.ComponentType<P>,
errorBoundaryOptions: ErrorBoundaryProps,
): React.FC<P> {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const componentDisplayName = WrappedComponent.displayName || WrappedComponent.name || UNKNOWN_COMPONENT;
const Wrapped: React.FC<P> = (props: P) => (
<ErrorBoundary {...errorBoundaryOptions}>
<WrappedComponent {...props} />
</ErrorBoundary>
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
Wrapped.displayName = `errorBoundary(${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
hoistNonReactStatics(Wrapped, WrappedComponent);
return Wrapped;
}
export { ErrorBoundary, withErrorBoundary };