Skip to content

fix(core): Prevent unhandled promise rejection in withMonitor #16753

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 1 addition & 2 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,8 @@ export function withMonitor<T>(
() => {
finishCheckIn('ok');
},
e => {
() => {
finishCheckIn('error');
throw e;
},
);
} else {
Expand Down
32 changes: 23 additions & 9 deletions packages/core/test/lib/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2215,19 +2215,33 @@ describe('Client', () => {
await expect(promise).resolves.toEqual(result);
});

// This test is skipped because jest keeps retrying ad infinitum
// when encountering an unhandled rejections.
// We could set "NODE_OPTIONS='--unhandled-rejections=warn' but it
// would affect the entire test suite.
// Maybe this can be re-enabled when switching to vitest.
//
// eslint-disable-next-line @sentry-internal/sdk/no-skipped-tests
test.skip('handles asynchronous errors', async () => {
test('handles asynchronous errors without causing unhandled rejection', async () => {
const error = new Error('Test error');
const callback = vi.fn().mockRejectedValue(error);

const promise = await withMonitor('test-monitor', callback);
const promise = withMonitor('test-monitor', callback);
await expect(promise).rejects.toThrowError(error);
});

test('handles promise rejection without causing unhandled rejection', async () => {
const error = new Error('Promise rejection');
const promise = withMonitor('test-monitor', () => Promise.reject(error));

await expect(promise).rejects.toThrow(error);
});

test('propagates promise values correctly', async () => {
const promise = withMonitor('test-monitor', () => Promise.resolve('resolved value'));
await expect(promise).resolves.toBe('resolved value');
});

test('handles nested withMonitor calls correctly', async () => {
const innerError = new Error('Inner error');
const promise = withMonitor('outer-monitor', () => {
return withMonitor('inner-monitor', () => Promise.reject(innerError));
});

await expect(promise).rejects.toThrow(innerError);
});
});
});
Loading