|
1 | 1 | import type { ReportDialogOptions } from '@sentry/browser';
|
2 |
| -import { captureException, getClient, showReportDialog, withScope } from '@sentry/browser'; |
| 2 | +import { getClient, showReportDialog, withScope } from '@sentry/browser'; |
3 | 3 | import type { Scope } from '@sentry/types';
|
4 |
| -import { isError, logger } from '@sentry/utils'; |
| 4 | +import { logger } from '@sentry/utils'; |
5 | 5 | import hoistNonReactStatics from 'hoist-non-react-statics';
|
6 | 6 | import * as React from 'react';
|
7 | 7 |
|
8 | 8 | import { DEBUG_BUILD } from './debug-build';
|
9 |
| - |
10 |
| -export function isAtLeastReact17(version: string): boolean { |
11 |
| - const major = version.match(/^([^.]+)/); |
12 |
| - return major !== null && parseInt(major[0]) >= 17; |
13 |
| -} |
| 9 | +import { captureReactException } from './error'; |
14 | 10 |
|
15 | 11 | export const UNKNOWN_COMPONENT = 'unknown';
|
16 | 12 |
|
@@ -69,25 +65,6 @@ const INITIAL_STATE = {
|
69 | 65 | eventId: null,
|
70 | 66 | };
|
71 | 67 |
|
72 |
| -function setCause(error: Error & { cause?: Error }, cause: Error): void { |
73 |
| - const seenErrors = new WeakMap<Error, boolean>(); |
74 |
| - |
75 |
| - function recurse(error: Error & { cause?: Error }, cause: Error): void { |
76 |
| - // If we've already seen the error, there is a recursive loop somewhere in the error's |
77 |
| - // cause chain. Let's just bail out then to prevent a stack overflow. |
78 |
| - if (seenErrors.has(error)) { |
79 |
| - return; |
80 |
| - } |
81 |
| - if (error.cause) { |
82 |
| - seenErrors.set(error, true); |
83 |
| - return recurse(error.cause, cause); |
84 |
| - } |
85 |
| - error.cause = cause; |
86 |
| - } |
87 |
| - |
88 |
| - recurse(error, cause); |
89 |
| -} |
90 |
| - |
91 | 68 | /**
|
92 | 69 | * A ErrorBoundary component that logs errors to Sentry.
|
93 | 70 | * NOTE: If you are a Sentry user, and you are seeing this stack frame, it means the
|
@@ -118,38 +95,15 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
|
118 | 95 | }
|
119 | 96 | }
|
120 | 97 |
|
121 |
| - public componentDidCatch(error: unknown, { componentStack }: React.ErrorInfo): void { |
| 98 | + public componentDidCatch(error: unknown, errorInfo: React.ErrorInfo): void { |
| 99 | + const { componentStack } = errorInfo; |
122 | 100 | const { beforeCapture, onError, showDialog, dialogOptions } = this.props;
|
123 | 101 | withScope(scope => {
|
124 |
| - // If on React version >= 17, create stack trace from componentStack param and links |
125 |
| - // to to the original error using `error.cause` otherwise relies on error param for stacktrace. |
126 |
| - // Linking errors requires the `LinkedErrors` integration be enabled. |
127 |
| - // See: https://reactjs.org/blog/2020/08/10/react-v17-rc.html#native-component-stacks |
128 |
| - // |
129 |
| - // Although `componentDidCatch` is typed to accept an `Error` object, it can also be invoked |
130 |
| - // with non-error objects. This is why we need to check if the error is an error-like object. |
131 |
| - // See: https://github.com/getsentry/sentry-javascript/issues/6167 |
132 |
| - if (isAtLeastReact17(React.version) && isError(error)) { |
133 |
| - const errorBoundaryError = new Error(error.message); |
134 |
| - errorBoundaryError.name = `React ErrorBoundary ${error.name}`; |
135 |
| - errorBoundaryError.stack = componentStack; |
136 |
| - |
137 |
| - // Using the `LinkedErrors` integration to link the errors together. |
138 |
| - setCause(error, errorBoundaryError); |
139 |
| - } |
140 |
| - |
141 | 102 | if (beforeCapture) {
|
142 | 103 | beforeCapture(scope, error, componentStack);
|
143 | 104 | }
|
144 | 105 |
|
145 |
| - const eventId = captureException(error, { |
146 |
| - captureContext: { |
147 |
| - contexts: { react: { componentStack } }, |
148 |
| - }, |
149 |
| - // If users provide a fallback component we can assume they are handling the error. |
150 |
| - // Therefore, we set the mechanism depending on the presence of the fallback prop. |
151 |
| - mechanism: { handled: !!this.props.fallback }, |
152 |
| - }); |
| 106 | + const eventId = captureReactException(error, errorInfo, { mechanism: { handled: !!this.props.fallback }}) |
153 | 107 |
|
154 | 108 | if (onError) {
|
155 | 109 | onError(error, componentStack, eventId);
|
|
0 commit comments