Skip to content

Commit

Permalink
fix(callbacks): Fix configureScope is safe (#2510)
Browse files Browse the repository at this point in the history
  • Loading branch information
krystofwoldrich committed Oct 3, 2022
1 parent 715eb31 commit cdd980c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
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,
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);

0 comments on commit cdd980c

Please sign in to comment.