Skip to content
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

fix(callbacks): Fix configureScope is safe #2510

Merged
merged 2 commits into from
Oct 3, 2022
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Make `configureScope` callback safe [#2510](https://github.com/getsentry/sentry-react-native/pull/2510)

## 4.6.0

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion src/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export {
captureException,
captureEvent,
captureMessage,
configureScope,
getHubFromCarrier,
getCurrentHub,
Hub,
Expand Down Expand Up @@ -66,6 +65,7 @@ export {
close,
captureUserFeedback,
withScope,
configureScope,
} from './sdk';
export { TouchEventBoundary, withTouchEventBoundary } from './touchevents';

Expand Down
15 changes: 15 additions & 0 deletions src/js/sdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,18 @@ export function withScope(callback: (scope: Scope) => void): ReturnType<Hub['wit
};
getCurrentHub().withScope(safeCallback);
}

/**
* Callback to set context information onto the scope.
* @param callback Callback function that receives Scope.
*/
export function configureScope(callback: (scope: Scope) => void): ReturnType<Hub['configureScope']> {
const safeCallback = (scope: Scope): void => {
try {
callback(scope);
} catch (e) {
logger.error('Error while running configureScope callback', e);
}
};
getCurrentHub().configureScope(safeCallback);
}
1 change: 0 additions & 1 deletion test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
envelopeItemPayload,
envelopeItems,
firstArg,
flushPromises,
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved
getMockSession,
getMockUserFeedback,
getSyncPromiseRejectOnFirstCall,
Expand Down
18 changes: 17 additions & 1 deletion test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface MockedClient {
}

let mockedGetCurrentHubWithScope: jest.Mock;
let mockedGetCurrentHubConfigureScope: jest.Mock;

jest.mock('@sentry/react', () => {
const actualModule = jest.requireActual('@sentry/react');
Expand All @@ -17,10 +18,12 @@ jest.mock('@sentry/react', () => {
...actualModule,
getCurrentHub: jest.fn(() => {
mockedGetCurrentHubWithScope = jest.fn();
mockedGetCurrentHubConfigureScope = jest.fn();
return {
getClient: jest.fn(() => mockClient),
setTag: jest.fn(),
withScope: mockedGetCurrentHubWithScope,
configureScope: mockedGetCurrentHubConfigureScope,
};
}),
defaultIntegrations: [ { name: 'MockedDefaultReactIntegration', setupOnce: jest.fn() } ],
Expand Down Expand Up @@ -62,7 +65,7 @@ import { getCurrentHub } from '@sentry/react';
import { Integration, Scope } from '@sentry/types';

import { ReactNativeClientOptions } from '../src/js/options';
import { flush, init, withScope } from '../src/js/sdk';
import { configureScope,flush, init, withScope } from '../src/js/sdk';
import { ReactNativeTracing, ReactNavigationInstrumentation } from '../src/js/tracing';
import { firstArg, secondArg } from './testutils';

Expand Down Expand Up @@ -234,6 +237,19 @@ describe('Tests the SDK functionality', () => {
});
});

describe('configureScope', () => {
test('configureScope callback does not throw', () => {
const mockScopeCallback = jest.fn(() => { throw 'Test error' });

configureScope(mockScopeCallback);

expect(() => {
(mockedGetCurrentHubConfigureScope.mock.calls[0][firstArg] as (scope: Scope) => void)({} as any);
}).not.toThrow();
expect(mockScopeCallback).toBeCalledTimes(1);
});
});

describe('integrations', () => {
it('replaces default integrations', () => {
const mockDefaultIntegration = getMockedIntegration();
Expand Down
2 changes: 0 additions & 2 deletions test/testutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,3 @@ export const getSyncPromiseRejectOnFirstCall = <Y extends any[]>(reason: unknown
}
});
};

export const flushPromises = (): Promise<void> => new Promise(setImmediate);