Skip to content

Commit

Permalink
fix(nestjs): do not make SentryTraced() decorated functions async (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
GitSquared authored Jul 11, 2024
1 parent 9d61b2c commit d304248
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ export class AppService1 {
}

@SentryTraced('return a string')
getString(): string {
return 'test';
getString(): { result: string } {
return { result: 'test' };
}

async testSpanDecoratorSync() {
return this.getString();
const returned = this.getString();
// Will fail if getString() is async, because returned will be a Promise<>
return returned.result;
}

/*
Expand Down
4 changes: 2 additions & 2 deletions packages/nestjs/src/span-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { startSpan } from '@sentry/node';
export function SentryTraced(op: string = 'function') {
return function (target: unknown, propertyKey: string, descriptor: PropertyDescriptor) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const originalMethod = descriptor.value as (...args: any[]) => Promise<any>;
const originalMethod = descriptor.value as (...args: any[]) => Promise<any> | any; // function can be sync or async

// eslint-disable-next-line @typescript-eslint/no-explicit-any
descriptor.value = function (...args: any[]) {
Expand All @@ -15,7 +15,7 @@ export function SentryTraced(op: string = 'function') {
op: op,
name: propertyKey,
},
async () => {
() => {
return originalMethod.apply(this, args);
},
);
Expand Down

0 comments on commit d304248

Please sign in to comment.