Skip to content

Commit 2cfb0ef

Browse files
lforstmydea
andauthored
feat(core): Deprecate hub capture APIs and add them to Scope (#10039)
Co-authored-by: Francesco Novy <[email protected]>
1 parent 548d455 commit 2cfb0ef

File tree

18 files changed

+434
-66
lines changed

18 files changed

+434
-66
lines changed

packages/core/src/exports.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,52 +30,51 @@ import { closeSession, makeSession, updateSession } from './session';
3030
import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent';
3131
import { parseEventHintOrCaptureContext } from './utils/prepareEvent';
3232

33-
// Note: All functions in this file are typed with a return value of `ReturnType<Hub[HUB_FUNCTION]>`,
34-
// where HUB_FUNCTION is some method on the Hub class.
35-
//
36-
// This is done to make sure the top level SDK methods stay in sync with the hub methods.
37-
// Although every method here has an explicit return type, some of them (that map to void returns) do not
38-
// contain `return` keywords. This is done to save on bundle size, as `return` is not minifiable.
39-
4033
/**
4134
* Captures an exception event and sends it to Sentry.
42-
* This accepts an event hint as optional second parameter.
43-
* Alternatively, you can also pass a CaptureContext directly as second parameter.
35+
*
36+
* @param exception The exception to capture.
37+
* @param hint Optinal additional data to attach to the Sentry event.
38+
* @returns the id of the captured Sentry event.
4439
*/
4540
export function captureException(
4641
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4742
exception: any,
4843
hint?: ExclusiveEventHintOrCaptureContext,
49-
): ReturnType<Hub['captureException']> {
44+
): string {
45+
// eslint-disable-next-line deprecation/deprecation
5046
return getCurrentHub().captureException(exception, parseEventHintOrCaptureContext(hint));
5147
}
5248

5349
/**
5450
* Captures a message event and sends it to Sentry.
5551
*
56-
* @param message The message to send to Sentry.
57-
* @param Severity Define the level of the message.
58-
* @returns The generated eventId.
52+
* @param exception The exception to capture.
53+
* @param captureContext Define the level of the message or pass in additional data to attach to the message.
54+
* @returns the id of the captured message.
5955
*/
6056
export function captureMessage(
6157
message: string,
6258
// eslint-disable-next-line deprecation/deprecation
6359
captureContext?: CaptureContext | Severity | SeverityLevel,
64-
): ReturnType<Hub['captureMessage']> {
60+
): string {
6561
// This is necessary to provide explicit scopes upgrade, without changing the original
6662
// arity of the `captureMessage(message, level)` method.
6763
const level = typeof captureContext === 'string' ? captureContext : undefined;
6864
const context = typeof captureContext !== 'string' ? { captureContext } : undefined;
65+
// eslint-disable-next-line deprecation/deprecation
6966
return getCurrentHub().captureMessage(message, level, context);
7067
}
7168

7269
/**
7370
* Captures a manually created event and sends it to Sentry.
7471
*
75-
* @param event The event to send to Sentry.
76-
* @returns The generated eventId.
72+
* @param exception The event to send to Sentry.
73+
* @param hint Optional additional data to attach to the Sentry event.
74+
* @returns the id of the captured event.
7775
*/
78-
export function captureEvent(event: Event, hint?: EventHint): ReturnType<Hub['captureEvent']> {
76+
export function captureEvent(event: Event, hint?: EventHint): string {
77+
// eslint-disable-next-line deprecation/deprecation
7978
return getCurrentHub().captureEvent(event, hint);
8079
}
8180

packages/core/src/hub.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ export class Hub implements HubInterface {
166166
public bindClient(client?: Client): void {
167167
const top = this.getStackTop();
168168
top.client = client;
169+
top.scope.setClient(client);
169170
if (client && client.setupIntegrations) {
170171
client.setupIntegrations();
171172
}
@@ -262,27 +263,26 @@ export class Hub implements HubInterface {
262263

263264
/**
264265
* @inheritDoc
266+
*
267+
* @deprecated Use `Sentry.captureException()` instead.
265268
*/
266269
public captureException(exception: unknown, hint?: EventHint): string {
267270
const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4());
268271
const syntheticException = new Error('Sentry syntheticException');
269-
this._withClient((client, scope) => {
270-
client.captureException(
271-
exception,
272-
{
273-
originalException: exception,
274-
syntheticException,
275-
...hint,
276-
event_id: eventId,
277-
},
278-
scope,
279-
);
272+
this.getScope().captureException(exception, {
273+
originalException: exception,
274+
syntheticException,
275+
...hint,
276+
event_id: eventId,
280277
});
278+
281279
return eventId;
282280
}
283281

284282
/**
285283
* @inheritDoc
284+
*
285+
* @deprecated Use `Sentry.captureMessage()` instead.
286286
*/
287287
public captureMessage(
288288
message: string,
@@ -292,34 +292,28 @@ export class Hub implements HubInterface {
292292
): string {
293293
const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4());
294294
const syntheticException = new Error(message);
295-
this._withClient((client, scope) => {
296-
client.captureMessage(
297-
message,
298-
level,
299-
{
300-
originalException: message,
301-
syntheticException,
302-
...hint,
303-
event_id: eventId,
304-
},
305-
scope,
306-
);
295+
this.getScope().captureMessage(message, level, {
296+
originalException: message,
297+
syntheticException,
298+
...hint,
299+
event_id: eventId,
307300
});
301+
308302
return eventId;
309303
}
310304

311305
/**
312306
* @inheritDoc
307+
*
308+
* @deprecated Use `Sentry.captureEvent()` instead.
313309
*/
314310
public captureEvent(event: Event, hint?: EventHint): string {
315311
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
316312
if (!event.type) {
317313
this._lastEventId = eventId;
318314
}
319315

320-
this._withClient((client, scope) => {
321-
client.captureEvent(event, { ...hint, event_id: eventId }, scope);
322-
});
316+
this.getScope().captureEvent(event, { ...hint, event_id: eventId });
323317
return eventId;
324318
}
325319

packages/core/src/scope.ts

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import type {
2424
Transaction,
2525
User,
2626
} from '@sentry/types';
27-
import { dateTimestampInSeconds, isPlainObject, uuid4 } from '@sentry/utils';
27+
import { dateTimestampInSeconds, isPlainObject, logger, uuid4 } from '@sentry/utils';
2828

2929
import { getGlobalEventProcessors, notifyEventProcessors } from './eventProcessors';
3030
import { updateSession } from './session';
@@ -581,6 +581,90 @@ export class Scope implements ScopeInterface {
581581
return this._propagationContext;
582582
}
583583

584+
/**
585+
* Capture an exception for this scope.
586+
*
587+
* @param exception The exception to capture.
588+
* @param hint Optinal additional data to attach to the Sentry event.
589+
* @returns the id of the captured Sentry event.
590+
*/
591+
public captureException(exception: unknown, hint?: EventHint): string {
592+
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
593+
594+
if (!this._client) {
595+
logger.warn('No client configured on scope - will not capture exception!');
596+
return eventId;
597+
}
598+
599+
const syntheticException = new Error('Sentry syntheticException');
600+
601+
this._client.captureException(
602+
exception,
603+
{
604+
originalException: exception,
605+
syntheticException,
606+
...hint,
607+
event_id: eventId,
608+
},
609+
this,
610+
);
611+
612+
return eventId;
613+
}
614+
615+
/**
616+
* Capture a message for this scope.
617+
*
618+
* @param message The message to capture.
619+
* @param level An optional severity level to report the message with.
620+
* @param hint Optional additional data to attach to the Sentry event.
621+
* @returns the id of the captured message.
622+
*/
623+
public captureMessage(message: string, level?: SeverityLevel, hint?: EventHint): string {
624+
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
625+
626+
if (!this._client) {
627+
logger.warn('No client configured on scope - will not capture message!');
628+
return eventId;
629+
}
630+
631+
const syntheticException = new Error(message);
632+
633+
this._client.captureMessage(
634+
message,
635+
level,
636+
{
637+
originalException: message,
638+
syntheticException,
639+
...hint,
640+
event_id: eventId,
641+
},
642+
this,
643+
);
644+
645+
return eventId;
646+
}
647+
648+
/**
649+
* Captures a manually created event for this scope and sends it to Sentry.
650+
*
651+
* @param exception The event to capture.
652+
* @param hint Optional additional data to attach to the Sentry event.
653+
* @returns the id of the captured event.
654+
*/
655+
public captureEvent(event: Event, hint?: EventHint): string {
656+
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
657+
658+
if (!this._client) {
659+
logger.warn('No client configured on scope - will not capture event!');
660+
return eventId;
661+
}
662+
663+
this._client.captureEvent(event, { ...hint, event_id: eventId }, this);
664+
665+
return eventId;
666+
}
667+
584668
/**
585669
* This will be called on every set call.
586670
*/

packages/core/src/tracing/transaction.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
153153
if (!transaction) {
154154
return undefined;
155155
}
156+
// eslint-disable-next-line deprecation/deprecation
156157
return this._hub.captureEvent(transaction);
157158
}
158159

packages/core/test/lib/integrations/metadata.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ describe('ModuleMetadata integration', () => {
6161
const client = new TestClient(options);
6262
const hub = getCurrentHub();
6363
hub.bindClient(client);
64+
// eslint-disable-next-line deprecation/deprecation
6465
hub.captureException(new Error('Some error'));
6566
});
6667
});

0 commit comments

Comments
 (0)