Skip to content

docs(js): Add note on nestjs OnEvent decorators #13734

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions docs/platforms/javascript/guides/nestjs/features/event-emitter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,26 @@ The NestJS SDK wraps the `@OnEvent` decorator automatically to:

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.

<Alert level="info" title="Multiple decorators">
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.

Instead, use one decorator per event name and handle any shared logic through a separate function.

```typescript
@OnEvent('event.A')
function myHandlerA(payload: any) {
commonHandler(payload)
}

@OnEvent('event.B')
function myHandlerB(payload: any) {
commonHandler(payload)
}

function commonHandler(payload: any) {
// handle stuff
}
```
</Alert>

This instrumentation works transparently with existing NestJS event handlers without requiring any code changes to your application.