diff --git a/packages/react/src/error.ts b/packages/react/src/error.ts index ce6d7a9cd2d5..dff809975dd2 100644 --- a/packages/react/src/error.ts +++ b/packages/react/src/error.ts @@ -1,4 +1,4 @@ -import { captureException, withScope } from '@sentry/browser'; +import { captureException } from '@sentry/browser'; import { isError } from '@sentry/core/browser'; import type { ErrorInfo } from 'react'; import { version } from 'react'; @@ -64,10 +64,7 @@ export function captureReactException( setCause(error, errorBoundaryError); } - return withScope(scope => { - scope.setContext('react', { componentStack }); - return captureException(error, hint); - }); + return captureException(error, hint); } /** diff --git a/packages/react/test/errorboundary.test.tsx b/packages/react/test/errorboundary.test.tsx index 3d2198e81dc9..2fbd258dfb2d 100644 --- a/packages/react/test/errorboundary.test.tsx +++ b/packages/react/test/errorboundary.test.tsx @@ -11,7 +11,6 @@ import type { ErrorBoundaryProps, FallbackRender } from '../src/errorboundary'; import { ErrorBoundary, UNKNOWN_COMPONENT, withErrorBoundary } from '../src/errorboundary'; const mockScope = new Scope(); -const scopeSetContextSpy = vi.spyOn(mockScope, 'setContext'); const mockCaptureException = vi.fn(); const mockShowReportDialog = vi.fn(); const mockClientOn = vi.fn(); @@ -388,16 +387,13 @@ describe('ErrorBoundary', () => { mechanism: { handled: true, type: 'auto.function.react.error_boundary' }, }); - expect(scopeSetContextSpy).toHaveBeenCalledTimes(1); - expect(scopeSetContextSpy).toHaveBeenCalledWith('react', { componentStack: expect.any(String) }); - expect(mockOnError.mock.calls[0]?.[0]).toEqual(mockCaptureException.mock.calls[0]?.[0]); // Check if error.cause -> react component stack const error = mockCaptureException.mock.calls[0]?.[0]; const cause = error.cause; - expect(cause.stack).toEqual(scopeSetContextSpy.mock.calls[0]?.[1]?.componentStack); + expect(cause.stack).toEqual(expect.any(String)); expect(cause.name).toContain('React ErrorBoundary'); expect(cause.message).toEqual(error.message); }); @@ -447,9 +443,6 @@ describe('ErrorBoundary', () => { mechanism: { handled: true, type: 'auto.function.react.error_boundary' }, }); - expect(scopeSetContextSpy).toHaveBeenCalledTimes(1); - expect(scopeSetContextSpy).toHaveBeenCalledWith('react', { componentStack: expect.any(String) }); - // Check if error.cause -> react component stack const error = mockCaptureException.mock.calls[0]?.[0]; expect(error.cause).not.toBeDefined(); @@ -486,16 +479,13 @@ describe('ErrorBoundary', () => { mechanism: { handled: true, type: 'auto.function.react.error_boundary' }, }); - expect(scopeSetContextSpy).toHaveBeenCalledTimes(1); - expect(scopeSetContextSpy).toHaveBeenCalledWith('react', { componentStack: expect.any(String) }); - expect(mockOnError.mock.calls[0]?.[0]).toEqual(mockCaptureException.mock.calls[0]?.[0]); const thirdError = mockCaptureException.mock.calls[0]?.[0]; const secondError = thirdError.cause; const firstError = secondError.cause; const cause = firstError.cause; - expect(cause.stack).toEqual(scopeSetContextSpy.mock.calls[0]?.[1]?.componentStack); + expect(cause.stack).toEqual(expect.any(String)); expect(cause.name).toContain('React ErrorBoundary'); expect(cause.message).toEqual(thirdError.message); }); @@ -530,15 +520,14 @@ describe('ErrorBoundary', () => { mechanism: { handled: true, type: 'auto.function.react.error_boundary' }, }); - expect(scopeSetContextSpy).toHaveBeenCalledTimes(1); - expect(scopeSetContextSpy).toHaveBeenCalledWith('react', { componentStack: expect.any(String) }); - expect(mockOnError.mock.calls[0]?.[0]).toEqual(mockCaptureException.mock.calls[0]?.[0]); const error = mockCaptureException.mock.calls[0]?.[0]; const cause = error.cause; - // We need to make sure that recursive error.cause does not cause infinite loop - expect(cause.stack).not.toEqual(scopeSetContextSpy.mock.calls[0]?.[1]?.componentStack); + // We need to make sure that recursive error.cause does not cause infinite loop: + // when the cause chain loops, captureReactException bails out of setCause and + // leaves the original (non-ErrorBoundary) cause intact instead of overwriting + // it with `errorBoundaryError`. expect(cause.name).not.toContain('React ErrorBoundary'); }); @@ -698,8 +687,6 @@ describe('ErrorBoundary', () => { mechanism: { handled: expected, type: 'auto.function.react.error_boundary' }, }); - expect(scopeSetContextSpy).toHaveBeenCalledTimes(1); - expect(scopeSetContextSpy).toHaveBeenCalledWith('react', { componentStack: expect.any(String) }); }, ); });