Skip to content

Commit 6828d30

Browse files
author
cod1k
committed
Handle promise rejection and synchronous errors in monitoring
Added error capturing for both synchronous and asynchronous exceptions in monitored methods. Incorporated `isThenable` checks to handle promises properly, ensuring robust error tracking with Sentry in both cases.
1 parent 74ea94a commit 6828d30

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

packages/core/src/exports.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,16 @@ export function withMonitor<T>(
175175
}
176176

177177
if (isThenable(maybePromiseResult)) {
178-
Promise.resolve(maybePromiseResult).then(
179-
() => {
178+
return maybePromiseResult.then(
179+
r => {
180180
finishCheckIn('ok');
181+
return r;
181182
},
182183
e => {
183184
finishCheckIn('error');
184185
throw e;
185186
},
186-
);
187+
) as T;
187188
} else {
188189
finishCheckIn('ok');
189190
}

packages/nestjs/src/decorators.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { MonitorConfig } from '@sentry/core';
2-
import { captureException } from '@sentry/core';
2+
import { captureException, isThenable } from '@sentry/core';
33
import * as Sentry from '@sentry/node';
44
import { startSpan } from '@sentry/node';
55
import { isExpectedError } from './helpers';
@@ -15,7 +15,20 @@ export const SentryCron = (monitorSlug: string, monitorConfig?: MonitorConfig):
1515
return Sentry.withMonitor(
1616
monitorSlug,
1717
() => {
18-
return originalMethod.apply(this, args);
18+
let result;
19+
try {
20+
result = originalMethod.apply(this, args);
21+
} catch (e) {
22+
captureException(e);
23+
throw e;
24+
}
25+
if (isThenable(result)) {
26+
return result.then(undefined, e => {
27+
captureException(e);
28+
throw e;
29+
});
30+
}
31+
return result;
1932
},
2033
monitorConfig,
2134
);

0 commit comments

Comments
 (0)