Skip to content
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
33 changes: 16 additions & 17 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,52 +26,51 @@ import type { Scope } from './scope';
import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent';
import { parseEventHintOrCaptureContext } from './utils/prepareEvent';

// Note: All functions in this file are typed with a return value of `ReturnType<Hub[HUB_FUNCTION]>`,
// where HUB_FUNCTION is some method on the Hub class.
//
// This is done to make sure the top level SDK methods stay in sync with the hub methods.
// Although every method here has an explicit return type, some of them (that map to void returns) do not
// contain `return` keywords. This is done to save on bundle size, as `return` is not minifiable.

/**
* Captures an exception event and sends it to Sentry.
* This accepts an event hint as optional second parameter.
* Alternatively, you can also pass a CaptureContext directly as second parameter.
*
* @param exception The exception to capture.
* @param hint Optinal additional data to attach to the Sentry event.
* @returns the id of the captured Sentry event.
*/
export function captureException(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
exception: any,
hint?: ExclusiveEventHintOrCaptureContext,
): ReturnType<Hub['captureException']> {
): string {
// eslint-disable-next-line deprecation/deprecation
return getCurrentHub().captureException(exception, parseEventHintOrCaptureContext(hint));
}

/**
* Captures a message event and sends it to Sentry.
*
* @param message The message to send to Sentry.
* @param Severity Define the level of the message.
* @returns The generated eventId.
* @param exception The exception to capture.
* @param captureContext Define the level of the message or pass in additional data to attach to the message.
* @returns the id of the captured message.
*/
export function captureMessage(
message: string,
// eslint-disable-next-line deprecation/deprecation
captureContext?: CaptureContext | Severity | SeverityLevel,
): ReturnType<Hub['captureMessage']> {
): string {
// This is necessary to provide explicit scopes upgrade, without changing the original
// arity of the `captureMessage(message, level)` method.
const level = typeof captureContext === 'string' ? captureContext : undefined;
const context = typeof captureContext !== 'string' ? { captureContext } : undefined;
// eslint-disable-next-line deprecation/deprecation
return getCurrentHub().captureMessage(message, level, context);
}

/**
* Captures a manually created event and sends it to Sentry.
*
* @param event The event to send to Sentry.
* @returns The generated eventId.
* @param exception The event to send to Sentry.
* @param hint Optional additional data to attach to the Sentry event.
* @returns the id of the captured event.
*/
export function captureEvent(event: Event, hint?: EventHint): ReturnType<Hub['captureEvent']> {
export function captureEvent(event: Event, hint?: EventHint): string {
// eslint-disable-next-line deprecation/deprecation
return getCurrentHub().captureEvent(event, hint);
}

Expand Down
46 changes: 20 additions & 26 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export class Hub implements HubInterface {
public bindClient(client?: Client): void {
const top = this.getStackTop();
top.client = client;
top.scope.setClient(client);
if (client && client.setupIntegrations) {
client.setupIntegrations();
}
Expand Down Expand Up @@ -262,27 +263,26 @@ export class Hub implements HubInterface {

/**
* @inheritDoc
*
* @deprecated Use `Sentry.captureException()` instead.
*/
public captureException(exception: unknown, hint?: EventHint): string {
const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4());
const syntheticException = new Error('Sentry syntheticException');
this._withClient((client, scope) => {
client.captureException(
exception,
{
originalException: exception,
syntheticException,
...hint,
event_id: eventId,
},
scope,
);
this.getScope().captureException(exception, {
originalException: exception,
syntheticException,
...hint,
event_id: eventId,
});

return eventId;
}

/**
* @inheritDoc
*
* @deprecated Use `Sentry.captureMessage()` instead.
*/
public captureMessage(
message: string,
Expand All @@ -292,34 +292,28 @@ export class Hub implements HubInterface {
): string {
const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4());
const syntheticException = new Error(message);
this._withClient((client, scope) => {
client.captureMessage(
message,
level,
{
originalException: message,
syntheticException,
...hint,
event_id: eventId,
},
scope,
);
this.getScope().captureMessage(message, level, {
originalException: message,
syntheticException,
...hint,
event_id: eventId,
});

return eventId;
}

/**
* @inheritDoc
*
* @deprecated Use `Sentry.captureEvent()` instead.
*/
public captureEvent(event: Event, hint?: EventHint): string {
const eventId = hint && hint.event_id ? hint.event_id : uuid4();
if (!event.type) {
this._lastEventId = eventId;
}

this._withClient((client, scope) => {
client.captureEvent(event, { ...hint, event_id: eventId }, scope);
});
this.getScope().captureEvent(event, { ...hint, event_id: eventId });
return eventId;
}

Expand Down
86 changes: 85 additions & 1 deletion packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
Transaction,
User,
} from '@sentry/types';
import { dateTimestampInSeconds, isPlainObject, uuid4 } from '@sentry/utils';
import { dateTimestampInSeconds, isPlainObject, logger, uuid4 } from '@sentry/utils';

import { getGlobalEventProcessors, notifyEventProcessors } from './eventProcessors';
import { updateSession } from './session';
Expand Down Expand Up @@ -581,6 +581,90 @@ export class Scope implements ScopeInterface {
return this._propagationContext;
}

/**
* Capture an exception for this scope.
*
* @param exception The exception to capture.
* @param hint Optinal additional data to attach to the Sentry event.
* @returns the id of the captured Sentry event.
*/
public captureException(exception: unknown, hint?: EventHint): string {
const eventId = hint && hint.event_id ? hint.event_id : uuid4();

if (!this._client) {
logger.warn('No client configured on scope - will not capture exception!');
return eventId;
}

const syntheticException = new Error('Sentry syntheticException');

this._client.captureException(
exception,
{
originalException: exception,
syntheticException,
...hint,
event_id: eventId,
},
this,
);

return eventId;
}

/**
* Capture a message for this scope.
*
* @param message The message to capture.
* @param level An optional severity level to report the message with.
* @param hint Optional additional data to attach to the Sentry event.
* @returns the id of the captured message.
*/
public captureMessage(message: string, level?: SeverityLevel, hint?: EventHint): string {
const eventId = hint && hint.event_id ? hint.event_id : uuid4();

if (!this._client) {
logger.warn('No client configured on scope - will not capture message!');
return eventId;
}

const syntheticException = new Error(message);

this._client.captureMessage(
message,
level,
{
originalException: message,
syntheticException,
...hint,
event_id: eventId,
},
this,
);

return eventId;
}

/**
* Captures a manually created event for this scope and sends it to Sentry.
*
* @param exception The event to capture.
* @param hint Optional additional data to attach to the Sentry event.
* @returns the id of the captured event.
*/
public captureEvent(event: Event, hint?: EventHint): string {
const eventId = hint && hint.event_id ? hint.event_id : uuid4();

if (!this._client) {
logger.warn('No client configured on scope - will not capture event!');
return eventId;
}

this._client.captureEvent(event, { ...hint, event_id: eventId }, this);

return eventId;
}

/**
* This will be called on every set call.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/tracing/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
if (!transaction) {
return undefined;
}
// eslint-disable-next-line deprecation/deprecation
return this._hub.captureEvent(transaction);
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/test/lib/integrations/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('ModuleMetadata integration', () => {
const client = new TestClient(options);
const hub = getCurrentHub();
hub.bindClient(client);
// eslint-disable-next-line deprecation/deprecation
hub.captureException(new Error('Some error'));
});
});
Loading