Skip to content

Commit a1933a7

Browse files
committed
fix react
1 parent b8c4e0a commit a1933a7

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

packages/react/src/errorboundary.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
125125
// See: https://github.com/getsentry/sentry-javascript/issues/6167
126126
if (isAtLeastReact17(React.version) && isError(error)) {
127127
const errorBoundaryError = new Error(error.message);
128-
errorBoundaryError.name = `React ErrorBoundary ${errorBoundaryError.name}`;
128+
// Once `errorBoundaryError` is constructed, the only thing we need its constructor for is its name. Since the
129+
// real constructor's `name` property is read-only, we just replace the entire constructor with an object that
130+
// has what we need.
131+
errorBoundaryError.constructor = {
132+
name: `React ErrorBoundary ${errorBoundaryError.name}`,
133+
} as typeof errorBoundaryError.constructor;
129134
errorBoundaryError.stack = componentStack;
130135

131136
// Using the `LinkedErrors` integration to link the errors together.

packages/react/test/errorboundary.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ describe('ErrorBoundary', () => {
247247
const error = mockCaptureException.mock.calls[0][0];
248248
const cause = error.cause;
249249
expect(cause.stack).toEqual(mockCaptureException.mock.calls[0][1].contexts.react.componentStack);
250-
expect(cause.name).toContain('React ErrorBoundary');
250+
expect(cause.constructor.name).toContain('React ErrorBoundary');
251251
expect(cause.message).toEqual(error.message);
252252
});
253253

@@ -339,7 +339,7 @@ describe('ErrorBoundary', () => {
339339
const firstError = secondError.cause;
340340
const cause = firstError.cause;
341341
expect(cause.stack).toEqual(mockCaptureException.mock.calls[0][1].contexts.react.componentStack);
342-
expect(cause.name).toContain('React ErrorBoundary');
342+
expect(cause.constructor.name).toContain('React ErrorBoundary');
343343
expect(cause.message).toEqual(thirdError.message);
344344
});
345345

@@ -379,7 +379,7 @@ describe('ErrorBoundary', () => {
379379
const cause = error.cause;
380380
// We need to make sure that recursive error.cause does not cause infinite loop
381381
expect(cause.stack).not.toEqual(mockCaptureException.mock.calls[0][1].contexts.react.componentStack);
382-
expect(cause.name).not.toContain('React ErrorBoundary');
382+
expect(cause.constructor.name).not.toContain('React ErrorBoundary');
383383
});
384384

385385
it('calls `beforeCapture()` when an error occurs', () => {

0 commit comments

Comments
 (0)