8000 fix(core): Prevent unhandled promise rejection in withMonitor · getsentry/sentry-javascript@7a13071 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a13071

Browse files
committed
fix(core): Prevent unhandled promise rejection in withMonitor
1 parent ec65e2a commit 7a13071

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

packages/core/src/exports.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,8 @@ export function withMonitor<T>(
179179
() => {
180180
finishCheckIn('ok');
181181
},
182-
e => {
182+
() => {
183183
finishCheckIn('error');
184-
throw e;
185184
},
186185
);
187186
} else {

packages/core/test/lib/client.test.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,19 +2215,33 @@ describe('Client', () => {
22152215
await expect(promise).resolves.toEqual(result);
22162216
});
22172217

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

2229-
const promise = await withMonitor('test-monitor', callback);
2222+
const promise = withMonitor('test-monitor', callback);
22302223
await expect(promise).rejects.toThrowError(error);
22312224
});
2225+
2226+
test('handles promise rejection without causing unhandled rejection', async () => {
2227+
const error = new Error('Promise rejection');
2228+
const promise = withMonitor('test-monitor', () => Promise.reject(error));
2229+
2230+
await expect(promise).rejects.toThrow(error);
2231+
});
2232+
2233+
test('propagates promise values correctly', async () => {
2234+
const promise = withMonitor('test-monitor', () => Promise.resolve('resolved value'));
2235+
await expect(promise).resolves.toBe('resolved value');
2236+
});
2237+
2238+
test('handles nested withMonitor calls correctly', async () => {
2239+
const innerError = new Error('Inner error');
2240+
const promise = withMonitor('outer-monitor', () => {
2241+
return withMonitor('inner-monitor', () => Promise.reject(innerError));
2242+
});
2243+
2244+
await expect(promise).rejects.toThrow(innerError);
2245+
});
22322246
});
22332247
});

0 commit comments

Comments
 (0)
0