Skip to content

ref(core): Skip hub in top level captureXXX methods #10688

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

Merged
merged 2 commits into from
Feb 16, 2024
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
9 changes: 3 additions & 6 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ export function captureException(
exception: any,
hint?: ExclusiveEventHintOrCaptureContext,
): string {
// eslint-disable-next-line deprecation/deprecation
return getCurrentHub().captureException(exception, parseEventHintOrCaptureContext(hint));
return getCurrentScope().captureException(exception, parseEventHintOrCaptureContext(hint));
}

/**
Expand All @@ -57,8 +56,7 @@ export function captureMessage(message: string, captureContext?: CaptureContext
// 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);
return getCurrentScope().captureMessage(message, level, context);
}

/**
Expand All @@ -69,8 +67,7 @@ export function captureMessage(message: string, captureContext?: CaptureContext
* @returns the id of the captured event.
*/
export function captureEvent(event: Event, hint?: EventHint): string {
// eslint-disable-next-line deprecation/deprecation
return getCurrentHub().captureEvent(event, hint);
return getCurrentScope().captureEvent(event, hint);
}

/**
Expand Down
25 changes: 10 additions & 15 deletions packages/node/test/onunhandledrejection.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import { Hub } from '@sentry/core';
import type { NodeClient } from '../src/client';
import * as SentryCore from '@sentry/core';
import type { Client } from '@sentry/types';

import { makeUnhandledPromiseHandler, onUnhandledRejectionIntegration } from '../src/integrations/onunhandledrejection';

// don't log the test errors we're going to throw, so at a quick glance it doesn't look like the test itself has failed
global.console.warn = () => null;
global.console.error = () => null;

const client = { getOptions: () => ({}) } as unknown as NodeClient;

jest.mock('@sentry/core', () => {
// we just want to short-circuit it, so dont worry about types
const original = jest.requireActual('@sentry/core');
return {
...original,
getClient: () => client,
};
});

describe('unhandled promises', () => {
test('install global listener', () => {
const client = { getOptions: () => ({}) } as unknown as Client;
SentryCore.setCurrentClient(client);

const integration = onUnhandledRejectionIntegration();
integration.setup!(client);
expect(process.listeners('unhandledRejection')).toHaveLength(1);
});

test('makeUnhandledPromiseHandler', () => {
const client = { getOptions: () => ({}) } as unknown as Client;
SentryCore.setCurrentClient(client);

const promise = {
domain: {
sentryContext: {
Expand All @@ -36,15 +31,15 @@ describe('unhandled promises', () => {
},
};

const captureException = jest.spyOn(Hub.prototype, 'captureException');
const captureException = jest.spyOn(SentryCore, 'captureException').mockImplementation(() => 'test');

const handler = makeUnhandledPromiseHandler(client, {
mode: 'warn',
});

handler('bla', promise);

expect(captureException.mock.calls[0][1]).toEqual({
expect(captureException).toHaveBeenCalledWith('bla', {
originalException: {
domain: {
sentryContext: {
Expand Down