Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,18 @@ export class SentryNestEventInstrumentation extends InstrumentationBase {
return decoratorResult(target, propertyKey, descriptor);
}

function eventNameFromEvent(event: unknown): string {
if (typeof event === 'string') {
return event;
} else if (Array.isArray(event)) {
return event.map(eventNameFromEvent).join(',');
} else return String(event);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Symbol Handling Fails in Event Name Extraction

The eventNameFromEvent function at line 82 uses String(event) in its fallback, which throws a TypeError when the event is a Symbol. This prevents proper Symbol handling, contrary to the PR's intent.

Fix in Cursor Fix in Web

Copy link
Contributor Author

@stefanvanderwolf stefanvanderwolf Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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

// Instrument the actual handler
descriptor.value = async function (...args: unknown[]) {
Expand All @@ -93,7 +101,7 @@ export class SentryNestEventInstrumentation extends InstrumentationBase {
eventName = eventData
.map((data: unknown) => {
if (data && typeof data === 'object' && 'event' in data && data.event) {
return data.event;
return eventNameFromEvent(data.event);
}
return '';
})
Expand Down
61 changes: 58 additions & 3 deletions packages/nestjs/test/integrations/nest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,72 @@ describe('Nest', () => {

await descriptor.value();

expect(core.startSpan).toHaveBeenCalled();
expect(core.startSpan).toHaveBeenCalledWith(
expect.objectContaining({
name: 'event test.event',
}),
expect.any(Function),
);
expect(originalHandler).toHaveBeenCalled();
});

it('should wrap array event handlers', async () => {
it('should wrap symbol event handlers', async () => {
const decorated = wrappedOnEvent(Symbol('test.event'));
decorated(mockTarget, 'testMethod', descriptor);

await descriptor.value();

expect(core.startSpan).toHaveBeenCalledWith(
expect.objectContaining({
name: 'event Symbol(test.event)',
}),
expect.any(Function),
);
expect(originalHandler).toHaveBeenCalled();
});

it('should wrap string array event handlers', async () => {
const decorated = wrappedOnEvent(['test.event1', 'test.event2']);
decorated(mockTarget, 'testMethod', descriptor);

await descriptor.value();

expect(core.startSpan).toHaveBeenCalled();
expect(core.startSpan).toHaveBeenCalledWith(
expect.objectContaining({
name: 'event test.event1,test.event2',
}),
expect.any(Function),
);
expect(originalHandler).toHaveBeenCalled();
});

it('should wrap symbol array event handlers', async () => {
const decorated = wrappedOnEvent([Symbol('test.event1'), Symbol('test.event2')]);
decorated(mockTarget, 'testMethod', descriptor);

await descriptor.value();

expect(core.startSpan).toHaveBeenCalledWith(
expect.objectContaining({
name: 'event Symbol(test.event1),Symbol(test.event2)',
}),
expect.any(Function),
);
expect(originalHandler).toHaveBeenCalled();
});

it('should wrap mixed type array event handlers', async () => {
const decorated = wrappedOnEvent([Symbol('test.event1'), 'test.event2', Symbol('test.event3')]);
decorated(mockTarget, 'testMethod', descriptor);

await descriptor.value();

expect(core.startSpan).toHaveBeenCalledWith(
expect.objectContaining({
name: 'event Symbol(test.event1),test.event2,Symbol(test.event3)',
}),
expect.any(Function),
);
expect(originalHandler).toHaveBeenCalled();
});

Expand Down
Loading