Skip to content

Commit 8a65711

Browse files
committed
revert nest.js change
1 parent 7b052e3 commit 8a65711

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

packages/nestjs/src/integrations/sentry-nest-event-instrumentation.ts

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
InstrumentationNodeModuleFile,
66
isWrapped,
77
} from '@opentelemetry/instrumentation';
8-
import { captureException, handleCallbackErrors, SDK_VERSION, startSpan } from '@sentry/core';
8+
import { captureException, SDK_VERSION, startSpan } from '@sentry/core';
99
import { getEventSpanOptions } from './helpers';
1010
import type { OnEventTarget } from './types';
1111

@@ -64,68 +64,70 @@ export class SentryNestEventInstrumentation extends InstrumentationBase {
6464

6565
// Return a new decorator function that wraps the handler
6666
return (target: OnEventTarget, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
67-
const originalHandler = descriptor.value;
68-
6967
if (
70-
!descriptorValueIsFunction(originalHandler) ||
68+
!descriptor.value ||
69+
typeof descriptor.value !== 'function' ||
7170
target.__SENTRY_INTERNAL__ ||
7271
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
7372
descriptor.value.__SENTRY_INSTRUMENTED__
7473
) {
7574
return decoratorResult(target, propertyKey, descriptor);
7675
}
7776

77+
const originalHandler = descriptor.value;
78+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
79+
const handlerName = originalHandler.name || propertyKey;
7880
let eventName = typeof event === 'string' ? event : String(event);
7981

8082
// Instrument the actual handler
81-
descriptor.value = new Proxy(originalHandler, {
82-
apply: async function (target, thisArg, args) {
83-
// When multiple @OnEvent decorators are used on a single method, we need to get all event names
84-
// from the reflector metadata as there is no information during execution which event triggered it
83+
descriptor.value = async function (...args: unknown[]) {
84+
// When multiple @OnEvent decorators are used on a single method, we need to get all event names
85+
// from the reflector metadata as there is no information during execution which event triggered it
86+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
87+
// @ts-ignore - reflect-metadata of nestjs adds these methods to Reflect
88+
if (Reflect.getMetadataKeys(descriptor.value).includes('EVENT_LISTENER_METADATA')) {
8589
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
8690
// @ts-ignore - reflect-metadata of nestjs adds these methods to Reflect
87-
if (Reflect.getMetadataKeys(descriptor.value).includes('EVENT_LISTENER_METADATA')) {
88-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
89-
// @ts-ignore - reflect-metadata of nestjs adds these methods to Reflect
90-
const eventData = Reflect.getMetadata('EVENT_LISTENER_METADATA', descriptor.value);
91-
if (Array.isArray(eventData)) {
92-
eventName = eventData
93-
.map((data: unknown) => {
94-
if (data && typeof data === 'object' && 'event' in data && data.event) {
95-
return data.event;
96-
}
97-
return '';
98-
})
99-
.reverse() // decorators are evaluated bottom to top
100-
.join('|');
101-
}
91+
const eventData = Reflect.getMetadata('EVENT_LISTENER_METADATA', descriptor.value);
92+
if (Array.isArray(eventData)) {
93+
eventName = eventData
94+
.map((data: unknown) => {
95+
if (data && typeof data === 'object' && 'event' in data && data.event) {
96+
return data.event;
97+
}
98+
return '';
99+
})
100+
.reverse() // decorators are evaluated bottom to top
101+
.join('|');
102102
}
103+
}
103104

104-
return startSpan(getEventSpanOptions(eventName), () => {
105-
return handleCallbackErrors(
106-
() => target.apply(thisArg, args),
107-
error => {
108-
captureException(error);
109-
},
110-
);
111-
});
112-
},
113-
});
105+
return startSpan(getEventSpanOptions(eventName), async () => {
106+
try {
107+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
108+
const result = await originalHandler.apply(this, args);
109+
return result;
110+
} catch (error) {
111+
// exceptions from event handlers are not caught by global error filter
112+
captureException(error);
113+
throw error;
114+
}
115+
});
116+
};
114117

115118
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
116119
descriptor.value.__SENTRY_INSTRUMENTED__ = true;
117120

121+
// Preserve the original function name
122+
Object.defineProperty(descriptor.value, 'name', {
123+
value: handlerName,
124+
configurable: true,
125+
});
126+
118127
// Apply the original decorator
119128
return decoratorResult(target, propertyKey, descriptor);
120129
};
121130
};
122131
};
123132
}
124133
}
125-
126-
function descriptorValueIsFunction(
127-
value: unknown,
128-
// eslint-disable-next-line @typescript-eslint/ban-types
129-
): value is Function & { __SENTRY_INSTRUMENTED__?: boolean; name?: string } {
130-
return !!value && typeof value === 'function';
131-
}

0 commit comments

Comments
 (0)