Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3324a90
Merge pull request #9853 from getsentry/master
github-actions[bot] Dec 14, 2023
383c816
build: Enable codecov AI PR review (#9850)
AbhiPrasad Dec 14, 2023
1e8d2b3
ref: Use `getCurrentScope()`/`hub.getScope()` instead of `configureSc…
mydea Dec 15, 2023
da8c9c3
feat(nextjs): Auto instrument generation functions (#9781)
Dec 15, 2023
30bb8b5
fix(utils): Do not use `Event` type in worldwide (#9864)
mydea Dec 15, 2023
ffc4181
ref: Use `getCurrentScope()` / `getClient` instead of `hub.xxx` (#9862)
mydea Dec 15, 2023
5bc9a38
fix(utils): Update `eventFromUnknownInput` to avoid scope pollution &…
mydea Dec 15, 2023
72c3488
ref(node): Refactor node integrations to use `processEvent` (#9018)
mydea Dec 15, 2023
e128936
fix(utils): Support crypto.getRandomValues in old Chromium versions (…
jghinestrosa Dec 15, 2023
53724c5
meta(feedback): Fix syntax error in README (#9875)
billyvg Dec 15, 2023
12c146b
fix(nextjs): Export `createReduxEnhancer` (#9854)
adam187 Dec 18, 2023
db4bef1
ref: Pass client instead of hub to `isSentryRequestUrl` (#9869)
mydea Dec 18, 2023
f2a4caa
feat(core): Update `withScope` to return callback return value (#9866)
mydea Dec 18, 2023
01a4cc9
feat(core): Add type & utility for function-based integrations (#9818)
mydea Dec 18, 2023
c0c5eca
feat(node): Add Hapi Integration (#9539)
onurtemizkan Dec 18, 2023
35906d0
ref: Use `addBreadcrumb` directly & allow to pass hint (#9867)
mydea Dec 18, 2023
f922414
build: Fix linting (#9888)
mydea Dec 18, 2023
e1d3633
ref(serverless): Avoid using `pushScope` (#9883)
mydea Dec 18, 2023
ada32b2
ref(nextjs): Simplify `wrapServerComponentWithSentry` (#9844)
Dec 18, 2023
0efdb21
feat(core): Deprecate `configureScope` (#9887)
mydea Dec 18, 2023
b27c236
feat(core): Deprecate `pushScope` & `popScope` (#9890)
mydea Dec 18, 2023
f121585
feat(nextjs): Connect server component transactions if there is no in…
Dec 18, 2023
8fb1a2f
feat(deno): Support `Deno.CronSchedule` for cron jobs (#9880)
timfish Dec 18, 2023
84299d0
feat(replay): Add `canvas.type` setting (#9877)
billyvg Dec 18, 2023
605fd50
feat(node-experimental): Update to new Scope APIs (#9799)
mydea Dec 19, 2023
32f4bf0
ref(deno): Refactor deno integration to avoid `setupOnce` (#9900)
mydea Dec 19, 2023
91a6b4e
ref(browser): Refactor browser integrations to avoid `setupOnce` (#9898)
mydea Dec 19, 2023
c9552a4
ref(node): Refactor LocalVariables integration to avoid `setupOnce` (…
mydea Dec 19, 2023
4e0c460
chore(sveltekit): Add SvelteKit 2.0 to peer dependencies (#9861)
Lms24 Dec 19, 2023
6d32228
fix(sveltekit): Add conditional exports (#9872)
Lms24 Dec 19, 2023
a856913
fix(remix): Do not capture thrown redirect responses. (#9909)
onurtemizkan Dec 19, 2023
31c769c
test(sveltekit): Add SvelteKit 2.0 E2E test app (#9873)
Lms24 Dec 19, 2023
cef3621
ref(sveltekit): Improve SvelteKit 2.0 404 server error handling (#9901)
Lms24 Dec 19, 2023
cf773fc
fix(sveltekit): Avoid capturing 404 errors on client side (#9902)
Lms24 Dec 19, 2023
9f173e1
meta(changelog): Update changelog for 7.89.0
Dec 19, 2023
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
Prev Previous commit
Next Next commit
feat(core): Update withScope to return callback return value (#9866)
To align this with OpenTelemetry and make some things possible that are
currently not easily doable without `pushScope` / `popScope`.

Noticed this because currently it's not easily possible to e.g. use
`withScope` in places like
[this](#9862 (comment)).



This should be backwards compatible because any code that previously
relied on this returning `void` should still work.
  • Loading branch information
mydea authored Dec 18, 2023
commit f2a4caab9a9e56b856a659742908c185c206ce31
4 changes: 2 additions & 2 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ export function setUser(user: User | null): ReturnType<Hub['setUser']> {
*
* @param callback that will be enclosed into push/popScope.
*/
export function withScope(callback: (scope: Scope) => void): ReturnType<Hub['withScope']> {
getCurrentHub().withScope(callback);
export function withScope<T>(callback: (scope: Scope) => T): T {
return getCurrentHub().withScope(callback);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ export class Hub implements HubInterface {
/**
* @inheritDoc
*/
public withScope(callback: (scope: Scope) => void): void {
public withScope<T>(callback: (scope: Scope) => T): T {
const scope = this.pushScope();
try {
callback(scope);
return callback(scope);
} finally {
this.popScope();
}
Expand Down
53 changes: 53 additions & 0 deletions packages/core/test/lib/exports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Hub, Scope, getCurrentScope, makeMain, withScope } from '../../src';
import { TestClient, getDefaultTestClientOptions } from '../mocks/client';

function getTestClient(): TestClient {
return new TestClient(
getDefaultTestClientOptions({
dsn: 'https://username@domain/123',
}),
);
}

describe('withScope', () => {
beforeEach(() => {
const client = getTestClient();
const hub = new Hub(client);
makeMain(hub);
});

it('works without a return value', () => {
const scope1 = getCurrentScope();
expect(scope1).toBeInstanceOf(Scope);

scope1.setTag('foo', 'bar');

const res = withScope(scope => {
expect(scope).toBeInstanceOf(Scope);
expect(scope).not.toBe(scope1);
expect(scope['_tags']).toEqual({ foo: 'bar' });

expect(getCurrentScope()).toBe(scope);
});

expect(getCurrentScope()).toBe(scope1);
expect(res).toBe(undefined);
});

it('works with a return value', () => {
const res = withScope(scope => {
return 'foo';
});

expect(res).toBe('foo');
});

it('works with an async function', async () => {
const res = withScope(async scope => {
return 'foo';
});

expect(res).toBeInstanceOf(Promise);
expect(await res).toBe('foo');
});
});
91 changes: 41 additions & 50 deletions packages/feedback/src/util/sendFeedbackRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,67 +33,58 @@ export async function sendFeedbackRequest(
type: 'feedback',
};

return new Promise((resolve, reject) => {
withScope(async scope => {
// No use for breadcrumbs in feedback
scope.clearBreadcrumbs();

if ([FEEDBACK_API_SOURCE, FEEDBACK_WIDGET_SOURCE].includes(String(source))) {
scope.setLevel('info');
}
return withScope(async scope => {
// No use for breadcrumbs in feedback
scope.clearBreadcrumbs();

if ([FEEDBACK_API_SOURCE, FEEDBACK_WIDGET_SOURCE].includes(String(source))) {
scope.setLevel('info');
}

const feedbackEvent = await prepareFeedbackEvent({
scope,
client,
event: baseEvent,
});

const feedbackEvent = await prepareFeedbackEvent({
scope,
client,
event: baseEvent,
});
if (!feedbackEvent) {
return;
}

if (!feedbackEvent) {
resolve();
return;
}
if (client.emit) {
client.emit('beforeSendFeedback', feedbackEvent, { includeReplay: Boolean(includeReplay) });
}

if (client.emit) {
client.emit('beforeSendFeedback', feedbackEvent, { includeReplay: Boolean(includeReplay) });
}
const envelope = createEventEnvelope(feedbackEvent, dsn, client.getOptions()._metadata, client.getOptions().tunnel);

const envelope = createEventEnvelope(
feedbackEvent,
dsn,
client.getOptions()._metadata,
client.getOptions().tunnel,
);
let response: void | TransportMakeRequestResponse;

let response: void | TransportMakeRequestResponse;
try {
response = await transport.send(envelope);
} catch (err) {
const error = new Error('Unable to send Feedback');

try {
response = await transport.send(envelope);
} catch (err) {
const error = new Error('Unable to send Feedback');

try {
// In case browsers don't allow this property to be writable
// @ts-expect-error This needs lib es2022 and newer
error.cause = err;
} catch {
// nothing to do
}
reject(error);
// In case browsers don't allow this property to be writable
// @ts-expect-error This needs lib es2022 and newer
error.cause = err;
} catch {
// nothing to do
}
throw error;
}

// TODO (v8): we can remove this guard once transport.send's type signature doesn't include void anymore
if (!response) {
resolve(response);
return;
}
// TODO (v8): we can remove this guard once transport.send's type signature doesn't include void anymore
if (!response) {
return;
}

// Require valid status codes, otherwise can assume feedback was not sent successfully
if (typeof response.statusCode === 'number' && (response.statusCode < 200 || response.statusCode >= 300)) {
reject(new Error('Unable to send Feedback'));
}
// Require valid status codes, otherwise can assume feedback was not sent successfully
if (typeof response.statusCode === 'number' && (response.statusCode < 200 || response.statusCode >= 300)) {
throw new Error('Unable to send Feedback');
}

resolve(response);
});
return response;
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface Hub {
*
* @param callback that will be enclosed into push/popScope.
*/
withScope(callback: (scope: Scope) => void): void;
withScope<T>(callback: (scope: Scope) => T): T;

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