Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Sentry = require('@sentry/node');

Sentry.init({
dsn: 'https://[email protected]/1337',
integrations: [Sentry.onUnhandledRejectionIntegration({ mode: 'none' })],
});

setTimeout(() => {
process.stdout.write("I'm alive!");
process.exit(0);
}, 500);

Promise.reject('test rejection');
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const Sentry = require('@sentry/node');

Sentry.init({
dsn: 'https://[email protected]/1337',
integrations: [Sentry.onUnhandledRejectionIntegration({ mode: 'strict' })],
});

setTimeout(() => {
// should not be called
process.stdout.write("I'm alive!");
process.exit(0);
}, 500);

Promise.reject('test rejection');
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Sentry = require('@sentry/node');

Sentry.init({
dsn: 'https://[email protected]/1337',
});

setTimeout(() => {
process.stdout.write("I'm alive!");
process.exit(0);
}, 500);

Promise.reject(new Error('test rejection'));
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Sentry = require('@sentry/node');

Sentry.init({
dsn: 'https://[email protected]/1337',
});

setTimeout(() => {
process.stdout.write("I'm alive!");
process.exit(0);
}, 500);

Promise.reject('test rejection');
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
transport: loggingTransport,
integrations: [Sentry.onUnhandledRejectionIntegration({ mode: 'strict' })],
});

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Promise.reject('test rejection');
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
transport: loggingTransport,
});

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Promise.reject('test rejection');
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import * as childProcess from 'child_process';
import * as path from 'path';
import { afterAll, describe, expect, test } from 'vitest';
import { cleanupChildProcesses } from '../../../utils/runner';
import { createRunner } from '../../../utils/runner';

describe('onUnhandledRejectionIntegration', () => {
afterAll(() => {
cleanupChildProcesses();
});

test('should show string-type promise rejection warnings by default', () =>
new Promise<void>(done => {
expect.assertions(3);

const testScriptPath = path.resolve(__dirname, 'mode-warn-string.js');

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout, stderr) => {
expect(err).toBeNull();
expect(stdout).toBe("I'm alive!");
expect(stderr.trim())
.toBe(`This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
test rejection`);
done();
});
}));

test('should show error-type promise rejection warnings by default', () =>
new Promise<void>(done => {
expect.assertions(3);

const testScriptPath = path.resolve(__dirname, 'mode-warn-error.js');

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout, stderr) => {
expect(err).toBeNull();
expect(stdout).toBe("I'm alive!");
expect(stderr)
.toContain(`This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
Error: test rejection
at Object.<anonymous>`);
done();
});
}));

test('should not close process on unhandled rejection in strict mode', () =>
new Promise<void>(done => {
expect.assertions(4);

const testScriptPath = path.resolve(__dirname, 'mode-strict.js');

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout, stderr) => {
expect(err).not.toBeNull();
expect(err?.code).toBe(1);
expect(stdout).not.toBe("I'm alive!");
expect(stderr)
.toContain(`This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
test rejection`);
done();
});
}));

test('should not close process or warn on unhandled rejection in none mode', () =>
new Promise<void>(done => {
expect.assertions(3);

const testScriptPath = path.resolve(__dirname, 'mode-none.js');

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout, stderr) => {
expect(err).toBeNull();
expect(stdout).toBe("I'm alive!");
expect(stderr).toBe('');
done();
});
}));

test('captures exceptions for unhandled rejections', async () => {
await createRunner(__dirname, 'scenario-warn.ts')
.expect({
event: {
level: 'error',
exception: {
values: [
{
type: 'Error',
value: 'test rejection',
mechanism: {
type: 'onunhandledrejection',
handled: false,
},
stacktrace: {
frames: expect.any(Array),
},
},
],
},
},
})
.start()
.completed();
});

test('captures exceptions for unhandled rejections in strict mode', async () => {
await createRunner(__dirname, 'scenario-strict.ts')
.expect({
event: {
level: 'fatal',
exception: {
values: [
{
type: 'Error',
value: 'test rejection',
mechanism: {
type: 'onunhandledrejection',
handled: false,
},
stacktrace: {
frames: expect.any(Array),
},
},
],
},
},
})
.start()
.completed();
});
});
20 changes: 13 additions & 7 deletions packages/node/src/integrations/onunhandledrejection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Client, IntegrationFn } from '@sentry/core';
import type { Client, IntegrationFn, SeverityLevel } from '@sentry/core';
import { captureException, consoleSandbox, defineIntegration, getClient } from '@sentry/core';
import { logAndExitProcess } from '../utils/errorhandling';

Expand All @@ -15,12 +15,15 @@ interface OnUnhandledRejectionOptions {
const INTEGRATION_NAME = 'OnUnhandledRejection';

const _onUnhandledRejectionIntegration = ((options: Partial<OnUnhandledRejectionOptions> = {}) => {
const mode = options.mode || 'warn';
const opts = {
mode: 'warn',
...options,
} satisfies OnUnhandledRejectionOptions;

return {
name: INTEGRATION_NAME,
setup(client) {
global.process.on('unhandledRejection', makeUnhandledPromiseHandler(client, { mode }));
global.process.on('unhandledRejection', makeUnhandledPromiseHandler(client, opts));
},
};
}) satisfies IntegrationFn;
Expand All @@ -46,25 +49,28 @@ export function makeUnhandledPromiseHandler(
return;
}

const level: SeverityLevel = options.mode === 'strict' ? 'fatal' : 'error';

captureException(reason, {
originalException: promise,
captureContext: {
extra: { unhandledPromiseRejection: true },
level,
},
mechanism: {
handled: false,
type: 'onunhandledrejection',
},
});

handleRejection(reason, options);
handleRejection(reason, options.mode);
};
}

/**
* Handler for `mode` option
*/
function handleRejection(reason: unknown, options: OnUnhandledRejectionOptions): void {
function handleRejection(reason: unknown, mode: UnhandledRejectionMode): void {
// https://github.com/nodejs/node/blob/7cf6f9e964aa00772965391c23acda6d71972a9a/lib/internal/process/promises.js#L234-L240
const rejectionWarning =
'This error originated either by ' +
Expand All @@ -73,12 +79,12 @@ function handleRejection(reason: unknown, options: OnUnhandledRejectionOptions):
' The promise rejected with the reason:';

/* eslint-disable no-console */
if (options.mode === 'warn') {
if (mode === 'warn') {
consoleSandbox(() => {
console.warn(rejectionWarning);
console.error(reason && typeof reason === 'object' && 'stack' in reason ? reason.stack : reason);
});
} else if (options.mode === 'strict') {
} else if (mode === 'strict') {
consoleSandbox(() => {
console.warn(rejectionWarning);
});
Expand Down
Loading