Skip to content
Open
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/node-core/src/integrations/onunhandledrejection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function makeUnhandledPromiseHandler(
client: Client,
options: OnUnhandledRejectionOptions,
): (reason: unknown, promise: unknown) => void {
return function sendUnhandledPromise(reason: unknown, promise: unknown): void {
return function sendUnhandledPromise(reason: unknown, _promise: unknown): void {
// Only handle for the active client
if (getClient() !== client) {
return;
Expand All @@ -109,7 +109,7 @@ export function makeUnhandledPromiseHandler(

activeSpanWrapper(() => {
captureException(reason, {
originalException: promise,
originalException: reason,
captureContext: {
extra: { unhandledPromiseRejection: true },
level,
Expand Down
54 changes: 54 additions & 0 deletions packages/node-core/test/integrations/onunhandledrejection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as SentryCore from '@sentry/core';
import type { Client } from '@sentry/core';
import { afterEach, describe, expect, it, vi } from 'vitest';
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;

describe('unhandled promises', () => {
Comment thread
logaretm marked this conversation as resolved.
afterEach(() => {
Comment thread
logaretm marked this conversation as resolved.
vi.restoreAllMocks();
});

it('installs a global listener', () => {
const client = { getOptions: () => ({}) } as unknown as Client;
SentryCore.setCurrentClient(client);

const beforeListeners = process.listeners('unhandledRejection').length;

const integration = onUnhandledRejectionIntegration();
integration.setup!(client);

expect(process.listeners('unhandledRejection').length).toBe(beforeListeners + 1);
Comment thread
logaretm marked this conversation as resolved.
});

it('passes the rejection reason (not the promise) as originalException', () => {
const client = { getOptions: () => ({}) } as unknown as Client;
SentryCore.setCurrentClient(client);

const reason = new Error('boom');
const promise = Promise.reject(reason);
// swallow the rejection so it does not leak into the test runner
promise.catch(() => {});

const captureException = vi.spyOn(SentryCore, 'captureException').mockImplementation(() => 'test');

const handler = makeUnhandledPromiseHandler(client, { mode: 'warn', ignore: [] });
handler(reason, promise);

expect(captureException).toHaveBeenCalledTimes(1);
const [capturedReason, hint] = captureException.mock.calls[0]!;
expect(capturedReason).toBe(reason);
expect(hint?.originalException).toBe(reason);
expect(hint?.originalException).not.toBe(promise);
expect(hint?.mechanism).toEqual({
handled: false,
type: 'auto.node.onunhandledrejection',
});
});
});
Loading