-
-
Notifications
You must be signed in to change notification settings - Fork 355
feat(replay): Add beforeErrorSampling callback to mobileReplayIntegration #5393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+317
−5
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
57c34f6
feat(replay): Add beforeErrorSampling callback to mobileReplayIntegra…
antonis 1632659
Adds changelog
antonis 9eac259
Add exception handling
antonis 8c195b6
Merge branch 'main' into antonis/replay-beforeErrorSampling
antonis 3278829
Update log message
antonis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,282 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals'; | ||
| import type { Event, EventHint } from '@sentry/core'; | ||
| import { mobileReplayIntegration } from '../../src/js/replay/mobilereplay'; | ||
| import * as environment from '../../src/js/utils/environment'; | ||
| import { NATIVE } from '../../src/js/wrapper'; | ||
|
|
||
| jest.mock('../../src/js/wrapper'); | ||
|
|
||
| describe('Mobile Replay Integration', () => { | ||
| let mockCaptureReplay: jest.MockedFunction<typeof NATIVE.captureReplay>; | ||
| let mockGetCurrentReplayId: jest.MockedFunction<typeof NATIVE.getCurrentReplayId>; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| jest.spyOn(environment, 'isExpoGo').mockReturnValue(false); | ||
| jest.spyOn(environment, 'notMobileOs').mockReturnValue(false); | ||
| mockCaptureReplay = NATIVE.captureReplay as jest.MockedFunction<typeof NATIVE.captureReplay>; | ||
| mockGetCurrentReplayId = NATIVE.getCurrentReplayId as jest.MockedFunction<typeof NATIVE.getCurrentReplayId>; | ||
| mockCaptureReplay.mockResolvedValue('test-replay-id'); | ||
| mockGetCurrentReplayId.mockReturnValue('test-replay-id'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe('beforeErrorSampling', () => { | ||
| it('should capture replay when beforeErrorSampling returns true', async () => { | ||
| const beforeErrorSampling = jest.fn<(event: Event, hint: EventHint) => boolean>().mockReturnValue(true); | ||
| const integration = mobileReplayIntegration({ beforeErrorSampling }); | ||
|
|
||
| const event: Event = { | ||
| event_id: 'test-event-id', | ||
| exception: { | ||
| values: [{ type: 'Error', value: 'Test error' }], | ||
| }, | ||
| }; | ||
| const hint: EventHint = {}; | ||
|
|
||
| if (integration.processEvent) { | ||
| await integration.processEvent(event, hint); | ||
| } | ||
|
|
||
| expect(beforeErrorSampling).toHaveBeenCalledWith(event, hint); | ||
| expect(mockCaptureReplay).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not capture replay when beforeErrorSampling returns false', async () => { | ||
| const beforeErrorSampling = jest.fn<(event: Event, hint: EventHint) => boolean>().mockReturnValue(false); | ||
| const integration = mobileReplayIntegration({ beforeErrorSampling }); | ||
|
|
||
| const event: Event = { | ||
| event_id: 'test-event-id', | ||
| exception: { | ||
| values: [{ type: 'Error', value: 'Test error' }], | ||
| }, | ||
| }; | ||
| const hint: EventHint = {}; | ||
|
|
||
| if (integration.processEvent) { | ||
| await integration.processEvent(event, hint); | ||
| } | ||
|
|
||
| expect(beforeErrorSampling).toHaveBeenCalledWith(event, hint); | ||
| expect(mockCaptureReplay).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should capture replay when beforeErrorSampling returns undefined', async () => { | ||
| const beforeErrorSampling = jest | ||
| .fn<(event: Event, hint: EventHint) => boolean>() | ||
| .mockReturnValue(undefined as unknown as boolean); | ||
| const integration = mobileReplayIntegration({ beforeErrorSampling }); | ||
|
|
||
| const event: Event = { | ||
| event_id: 'test-event-id', | ||
| exception: { | ||
| values: [{ type: 'Error', value: 'Test error' }], | ||
| }, | ||
| }; | ||
| const hint: EventHint = {}; | ||
|
|
||
| if (integration.processEvent) { | ||
| await integration.processEvent(event, hint); | ||
| } | ||
|
|
||
| expect(beforeErrorSampling).toHaveBeenCalledWith(event, hint); | ||
| expect(mockCaptureReplay).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should capture replay when beforeErrorSampling is not provided', async () => { | ||
| const integration = mobileReplayIntegration(); | ||
|
|
||
| const event: Event = { | ||
| event_id: 'test-event-id', | ||
| exception: { | ||
| values: [{ type: 'Error', value: 'Test error' }], | ||
| }, | ||
| }; | ||
| const hint: EventHint = {}; | ||
|
|
||
| if (integration.processEvent) { | ||
| await integration.processEvent(event, hint); | ||
| } | ||
|
|
||
| expect(mockCaptureReplay).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should filter out specific error types using beforeErrorSampling', async () => { | ||
| const beforeErrorSampling = jest.fn<(event: Event, hint: EventHint) => boolean>((event: Event) => { | ||
| // Only capture replays for unhandled errors (not manually captured) | ||
| const isHandled = event.exception?.values?.some(exception => exception.mechanism?.handled === true); | ||
| return !isHandled; | ||
| }); | ||
| const integration = mobileReplayIntegration({ beforeErrorSampling }); | ||
|
|
||
| // Test with handled error | ||
| const handledEvent: Event = { | ||
| event_id: 'handled-event-id', | ||
| exception: { | ||
| values: [ | ||
| { | ||
| type: 'Error', | ||
| value: 'Handled error', | ||
| mechanism: { handled: true, type: 'generic' }, | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
| const hint: EventHint = {}; | ||
|
|
||
| if (integration.processEvent) { | ||
| await integration.processEvent(handledEvent, hint); | ||
| } | ||
|
|
||
| expect(beforeErrorSampling).toHaveBeenCalledWith(handledEvent, hint); | ||
| expect(mockCaptureReplay).not.toHaveBeenCalled(); | ||
|
|
||
| jest.clearAllMocks(); | ||
|
|
||
| // Test with unhandled error | ||
| const unhandledEvent: Event = { | ||
| event_id: 'unhandled-event-id', | ||
| exception: { | ||
| values: [ | ||
| { | ||
| type: 'Error', | ||
| value: 'Unhandled error', | ||
| mechanism: { handled: false, type: 'generic' }, | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| if (integration.processEvent) { | ||
| await integration.processEvent(unhandledEvent, hint); | ||
| } | ||
|
|
||
| expect(beforeErrorSampling).toHaveBeenCalledWith(unhandledEvent, hint); | ||
| expect(mockCaptureReplay).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not call beforeErrorSampling for non-error events', async () => { | ||
| const beforeErrorSampling = jest.fn<(event: Event, hint: EventHint) => boolean>().mockReturnValue(false); | ||
| const integration = mobileReplayIntegration({ beforeErrorSampling }); | ||
|
|
||
| const event: Event = { | ||
| event_id: 'test-event-id', | ||
| message: 'Test message without exception', | ||
| }; | ||
| const hint: EventHint = {}; | ||
|
|
||
| if (integration.processEvent) { | ||
| await integration.processEvent(event, hint); | ||
| } | ||
|
|
||
| expect(beforeErrorSampling).not.toHaveBeenCalled(); | ||
| expect(mockCaptureReplay).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should handle exceptions thrown by beforeErrorSampling and proceed with capture', async () => { | ||
| const beforeErrorSampling = jest.fn<(event: Event, hint: EventHint) => boolean>().mockImplementation(() => { | ||
| throw new Error('Callback error'); | ||
| }); | ||
| const integration = mobileReplayIntegration({ beforeErrorSampling }); | ||
|
|
||
| const event: Event = { | ||
| event_id: 'test-event-id', | ||
| exception: { | ||
| values: [{ type: 'Error', value: 'Test error' }], | ||
| }, | ||
| }; | ||
| const hint: EventHint = {}; | ||
|
|
||
| if (integration.processEvent) { | ||
| await integration.processEvent(event, hint); | ||
| } | ||
|
|
||
| expect(beforeErrorSampling).toHaveBeenCalledWith(event, hint); | ||
| // Should proceed with replay capture despite callback error | ||
| expect(mockCaptureReplay).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not crash the event pipeline when beforeErrorSampling throws', async () => { | ||
| const beforeErrorSampling = jest.fn<(event: Event, hint: EventHint) => boolean>().mockImplementation(() => { | ||
| throw new TypeError('Unexpected callback error'); | ||
| }); | ||
| const integration = mobileReplayIntegration({ beforeErrorSampling }); | ||
|
|
||
| const event: Event = { | ||
| event_id: 'test-event-id', | ||
| exception: { | ||
| values: [{ type: 'Error', value: 'Test error' }], | ||
| }, | ||
| }; | ||
| const hint: EventHint = {}; | ||
|
|
||
| // Should not throw | ||
| if (integration.processEvent) { | ||
| await expect(integration.processEvent(event, hint)).resolves.toBeDefined(); | ||
| } | ||
|
|
||
| expect(beforeErrorSampling).toHaveBeenCalled(); | ||
| expect(mockCaptureReplay).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('processEvent', () => { | ||
| it('should not process events without exceptions', async () => { | ||
| const integration = mobileReplayIntegration(); | ||
|
|
||
| const event: Event = { | ||
| event_id: 'test-event-id', | ||
| message: 'Test message', | ||
| }; | ||
| const hint: EventHint = {}; | ||
|
|
||
| if (integration.processEvent) { | ||
| await integration.processEvent(event, hint); | ||
| } | ||
|
|
||
| expect(mockCaptureReplay).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should process events with exceptions', async () => { | ||
| const integration = mobileReplayIntegration(); | ||
|
|
||
| const event: Event = { | ||
| event_id: 'test-event-id', | ||
| exception: { | ||
| values: [{ type: 'Error', value: 'Test error' }], | ||
| }, | ||
| }; | ||
| const hint: EventHint = {}; | ||
|
|
||
| if (integration.processEvent) { | ||
| await integration.processEvent(event, hint); | ||
| } | ||
|
|
||
| expect(mockCaptureReplay).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('platform checks', () => { | ||
| it('should return noop integration in Expo Go', () => { | ||
| jest.spyOn(environment, 'isExpoGo').mockReturnValue(true); | ||
|
|
||
| const integration = mobileReplayIntegration(); | ||
|
|
||
| expect(integration.name).toBe('MobileReplay'); | ||
| expect(integration.processEvent).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return noop integration on non-mobile platforms', () => { | ||
| jest.spyOn(environment, 'notMobileOs').mockReturnValue(true); | ||
|
|
||
| const integration = mobileReplayIntegration(); | ||
|
|
||
| expect(integration.name).toBe('MobileReplay'); | ||
| expect(integration.processEvent).toBeUndefined(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.