Skip to content

Commit 94b806c

Browse files
fix: Add mechanism to unhandled rejections (#4457)
1 parent 7e87527 commit 94b806c

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Fixes
1212

13+
- Add mechanism field to unhandled rejection errors ([#4457](https://github.com/getsentry/sentry-react-native/pull/4457))
1314
- Use proper SDK name for Session Replay tags ([#4428](https://github.com/getsentry/sentry-react-native/pull/4428))
1415
- Use `makeDsn` from `core` to extract the URL from DSN avoiding unimplemented `URL.protocol` errors ([#4395](https://github.com/getsentry/sentry-react-native/pull/4395))
1516

packages/core/src/js/integrations/reactnativeerrorhandlers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function attachUnhandledRejectionHandler(): void {
8181
data: { id },
8282
originalException: error,
8383
syntheticException: isErrorLike(error) ? undefined : createSyntheticError(),
84+
mechanism: { handled: true, type: 'onunhandledrejection' },
8485
});
8586
},
8687
onHandled: (id: string) => {

packages/core/test/integrations/reactnativeerrorhandlers.test.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
jest.mock('../../src/js/integrations/reactnativeerrorhandlersutils');
22

3-
import type { ExtendedError, SeverityLevel } from '@sentry/core';
3+
import type { ExtendedError, Mechanism, SeverityLevel } from '@sentry/core';
44
import { setCurrentClient } from '@sentry/core';
55

66
import { reactNativeErrorHandlersIntegration } from '../../src/js/integrations/reactnativeerrorhandlers';
@@ -123,5 +123,28 @@ describe('ReactNativeErrorHandlers', () => {
123123
expect(mockEnable).toHaveBeenCalledTimes(1);
124124
expect(actualSyntheticError).toBeUndefined();
125125
});
126+
127+
test('unhandled rejected sets error mechanism', async () => {
128+
const integration = reactNativeErrorHandlersIntegration();
129+
integration.setupOnce();
130+
131+
const [actualTrackingOptions] = mockEnable.mock.calls[0] || [];
132+
actualTrackingOptions?.onUnhandled?.(1, 'Test Error');
133+
134+
await client.flush();
135+
const actualSyntheticError = client.hint?.syntheticException;
136+
const errorMechanism = client.event?.exception?.values[0]?.mechanism;
137+
expect(mockDisable).not.toHaveBeenCalled();
138+
expect(mockEnable).toHaveBeenCalledWith(
139+
expect.objectContaining({
140+
allRejections: true,
141+
onUnhandled: expect.any(Function),
142+
onHandled: expect.any(Function),
143+
}),
144+
);
145+
expect(mockEnable).toHaveBeenCalledTimes(1);
146+
expect((actualSyntheticError as ExtendedError).framesToPop).toBe(3);
147+
expect(errorMechanism).toEqual({ handled: true, type: 'onunhandledrejection' } as Mechanism);
148+
});
126149
});
127150
});

0 commit comments

Comments
 (0)