Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ test('Catches errors caught by error boundary', async ({ page }) => {

expect(errorEvent.exception?.values).toHaveLength(2);
expect(errorEvent.exception?.values?.[0]?.value).toBe('caught error');
expect(errorEvent.exception?.values?.[0]?.mechanism).toEqual({
type: 'auto.function.react.error_handler',
handled: true, // true because a callback was provided
exception_id: 1,
parent_id: 0,
source: 'cause',
});

expect(errorEvent.exception?.values?.[1]?.value).toBe('caught error');
expect(errorEvent.exception?.values?.[1]?.mechanism).toEqual({
type: 'generic',
handled: true, // true because a callback was provided
exception_id: 0,
});
});

test('Catches errors uncaught by error boundary', async ({ page }) => {
Expand All @@ -39,4 +53,18 @@ test('Catches errors uncaught by error boundary', async ({ page }) => {

expect(errorEvent.exception?.values).toHaveLength(2);
expect(errorEvent.exception?.values?.[0]?.value).toBe('uncaught error');
expect(errorEvent.exception?.values?.[0]?.mechanism).toEqual({
type: 'auto.function.react.error_handler',
handled: true, // true because a callback was provided
exception_id: 1,
parent_id: 0,
source: 'cause',
});

expect(errorEvent.exception?.values?.[1]?.value).toBe('uncaught error');
expect(errorEvent.exception?.values?.[1]?.mechanism).toEqual({
type: 'generic',
handled: true, // true because a callback was provided
exception_id: 0,
});
});
7 changes: 5 additions & 2 deletions packages/react/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ export function reactErrorHandler(
): (error: any, errorInfo: ErrorInfo) => void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (error: any, errorInfo: ErrorInfo) => {
const eventId = captureReactException(error, errorInfo);
if (callback) {
const hasCallback = !!callback;
const eventId = captureReactException(error, errorInfo, {
mechanism: { handled: hasCallback, type: 'auto.function.react.error_handler' },
});
if (hasCallback) {
callback(error, errorInfo, eventId);
}
};
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/errorboundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
}

const handled = this.props.handled != null ? this.props.handled : !!this.props.fallback;
const eventId = captureReactException(error, errorInfo, { mechanism: { handled } });
const eventId = captureReactException(error, errorInfo, {
mechanism: { handled, type: 'auto.function.react.error_boundary' },
});

if (onError) {
onError(error, componentStack, eventId);
Expand Down
47 changes: 45 additions & 2 deletions packages/react/test/error.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, test } from 'vitest';
import { isAtLeastReact17 } from '../src/error';
import * as SentryBrowser from '@sentry/browser';
import { beforeEach, describe, expect, it, test, vi } from 'vitest';
import { isAtLeastReact17, reactErrorHandler } from '../src/error';

describe('isAtLeastReact17', () => {
test.each([
Expand All @@ -13,3 +14,45 @@ describe('isAtLeastReact17', () => {
expect(isAtLeastReact17(input)).toBe(output);
});
});

describe('reactErrorHandler', () => {
const captureException = vi.spyOn(SentryBrowser, 'captureException');

beforeEach(() => {
captureException.mockClear();
});

it('captures errors as unhandled when no callback is provided', () => {
const error = new Error('test error');
const errorInfo = { componentStack: 'component stack' };

const handler = reactErrorHandler();

handler(error, errorInfo);

expect(captureException).toHaveBeenCalledTimes(1);
expect(captureException).toHaveBeenCalledWith(error, {
mechanism: { handled: false, type: 'auto.function.react.error_handler' },
});
});

it('captures errors as handled when a callback is provided', () => {
captureException.mockReturnValueOnce('custom-event-id');

const error = new Error('test error');
const errorInfo = { componentStack: 'component stack' };

const callback = vi.fn();
const handler = reactErrorHandler(callback);

handler(error, errorInfo);

expect(captureException).toHaveBeenCalledTimes(1);
expect(captureException).toHaveBeenCalledWith(error, {
mechanism: { handled: true, type: 'auto.function.react.error_handler' },
});

expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(error, errorInfo, 'custom-event-id');
});
});
10 changes: 5 additions & 5 deletions packages/react/test/errorboundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ describe('ErrorBoundary', () => {

expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException).toHaveBeenLastCalledWith(expect.any(Error), {
mechanism: { handled: true },
mechanism: { handled: true, type: 'auto.function.react.error_boundary' },
});

expect(scopeSetContextSpy).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -444,7 +444,7 @@ describe('ErrorBoundary', () => {

expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException).toHaveBeenLastCalledWith('bam', {
mechanism: { handled: true },
mechanism: { handled: true, type: 'auto.function.react.error_boundary' },
});

expect(scopeSetContextSpy).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -483,7 +483,7 @@ describe('ErrorBoundary', () => {

expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException).toHaveBeenLastCalledWith(expect.any(Error), {
mechanism: { handled: true },
mechanism: { handled: true, type: 'auto.function.react.error_boundary' },
});

expect(scopeSetContextSpy).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -527,7 +527,7 @@ describe('ErrorBoundary', () => {

expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException).toHaveBeenLastCalledWith(expect.any(Error), {
mechanism: { handled: true },
mechanism: { handled: true, type: 'auto.function.react.error_boundary' },
});

expect(scopeSetContextSpy).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -695,7 +695,7 @@ describe('ErrorBoundary', () => {

expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException).toHaveBeenLastCalledWith(expect.any(Object), {
mechanism: { handled: expected },
mechanism: { handled: expected, type: 'auto.function.react.error_boundary' },
});

expect(scopeSetContextSpy).toHaveBeenCalledTimes(1);
Expand Down