Skip to content

Commit daaadde

Browse files
committed
add note on decorators
1 parent 984545e commit daaadde

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/platforms/javascript/guides/nestjs/features/event-emitter.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,26 @@ The NestJS SDK wraps the `@OnEvent` decorator automatically to:
1515

1616
When an event handler is executed, a new span is created to track its performance, and any errors are automatically reported to Sentry while preserving the original error behavior.
1717

18+
<Alert level="info" title="Multiple decorators">
19+
Annotating one function with multiple `@OnEvent` decorators is not recommended, as NestJS provides no way for us to determine the triggering event. Therefore, the resulting span name will include all decorated event names.
20+
21+
Instead, use one decorator per event name and handle any shared logic through a separate function.
22+
23+
```typescript
24+
@OnEvent('event.A')
25+
function myHandlerA(payload: any) {
26+
commonHandler(payload)
27+
}
28+
29+
@OnEvent('event.B')
30+
function myHandlerB(payload: any) {
31+
commonHandler(payload)
32+
}
33+
34+
function commonHandler(payload: any) {
35+
// handle stuff
36+
}
37+
```
38+
</Alert>
39+
1840
This instrumentation works transparently with existing NestJS event handlers without requiring any code changes to your application.

0 commit comments

Comments
 (0)