Skip to content

Commit d304248

Browse files
authored
fix(nestjs): do not make SentryTraced() decorated functions async (#12879)
1 parent 9d61b2c commit d304248

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

dev-packages/e2e-tests/test-applications/nestjs/src/app.service.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ export class AppService1 {
9999
}
100100

101101
@SentryTraced('return a string')
102-
getString(): string {
103-
return 'test';
102+
getString(): { result: string } {
103+
return { result: 'test' };
104104
}
105105

106106
async testSpanDecoratorSync() {
107-
return this.getString();
107+
const returned = this.getString();
108+
// Will fail if getString() is async, because returned will be a Promise<>
109+
return returned.result;
108110
}
109111

110112
/*

packages/nestjs/src/span-decorator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { startSpan } from '@sentry/node';
66
export function SentryTraced(op: string = 'function') {
77
return function (target: unknown, propertyKey: string, descriptor: PropertyDescriptor) {
88
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9-
const originalMethod = descriptor.value as (...args: any[]) => Promise<any>;
9+
const originalMethod = descriptor.value as (...args: any[]) => Promise<any> | any; // function can be sync or async
1010

1111
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1212
descriptor.value = function (...args: any[]) {
@@ -15,7 +15,7 @@ export function SentryTraced(op: string = 'function') {
1515
op: op,
1616
name: propertyKey,
1717
},
18-
async () => {
18+
() => {
1919
return originalMethod.apply(this, args);
2020
},
2121
);

0 commit comments

Comments
 (0)