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

feat: withScope returns from wrapped function #3753

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions packages/hub/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ export class Hub implements HubInterface {
/**
* @inheritDoc
*/
public withScope(callback: (scope: Scope) => void): void {
public withScope<T>(fn: (scope: Scope) => T): T {
const scope = this.pushScope();
try {
callback(scope);
return fn(scope);
} finally {
this.popScope();
rhcarvalho marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
12 changes: 12 additions & 0 deletions packages/hub/test/hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ describe('Hub', () => {
});
}).toThrow(error);
});

test('should return return value from wrapped function', () => {
// someFn represents an existing function
const someFn = () => {
const hub = getCurrentHub();
hub.setTag('key', 'value');
hub.captureMessage('test');
return 'ok';
};
const value = hub.withScope(someFn); // runs someFn in a new scope
expect(value).toBe('ok');
});
});

test('getCurrentClient', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/minimal/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ export function setUser(user: User | null): void {
* This is essentially a convenience function for:
*
* pushScope();
* callback();
* fn();
* popScope();
*
* @param callback that will be enclosed into push/popScope.
* @param fn wrapped function.
*/
export function withScope(callback: (scope: Scope) => void): void {
callOnHub<void>('withScope', callback);
export function withScope<T>(fn: (scope: Scope) => T): T {
return callOnHub<T>('withScope', fn);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/minimal/test/lib/minimal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ describe('Minimal', () => {
});

test('withScope', () => {
withScope(scope => {
const value = withScope(scope => {
scope.setLevel(Severity.Warning);
scope.setFingerprint(['1']);
withScope(scope2 => {
Expand All @@ -261,8 +261,10 @@ describe('Minimal', () => {
expect(global.__SENTRY__.hub._stack).toHaveLength(3);
});
expect(global.__SENTRY__.hub._stack).toHaveLength(2);
return 'ok';
});
expect(global.__SENTRY__.hub._stack).toHaveLength(1);
expect(value).toBe('ok');
});

test('setExtras', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/types/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ export interface Hub {
* This is essentially a convenience function for:
*
* pushScope();
* callback();
* fn();
* popScope();
*
* @param callback that will be enclosed into push/popScope.
* @param fn wrapped function.
*/
withScope(callback: (scope: Scope) => void): void;
withScope<T>(fn: (scope: Scope) => T): T;

/** Returns the client of the top stack. */
getClient(): Client | undefined;
Expand Down