Skip to content

feat(nestjs): Support WebSocket errors in SentryGlobalFilter#22224

Open
psh4607 wants to merge 4 commits into
getsentry:developfrom
psh4607:feat/nestjs-ws-global-filter
Open

feat(nestjs): Support WebSocket errors in SentryGlobalFilter#22224
psh4607 wants to merge 4 commits into
getsentry:developfrom
psh4607:feat/nestjs-ws-global-filter

Conversation

@psh4607

@psh4607 psh4607 commented Jul 13, 2026

Copy link
Copy Markdown

Before submitting a pull request, please take a look at our
Contributing guidelines and verify:

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).
  • Link an issue if there is one related to your pull request. If no issue is linked, one will be auto-generated and linked.

Fixes #16067

SentryGlobalFilter now handles WebSocket exceptions without delegating them to Nest's HTTP BaseExceptionFilter.

Unexpected gateway errors are captured with the auto.ws.nestjs.global_filter mechanism and emit a generic error response. Expected WsException responses are preserved without being reported to Sentry. WsException is detected by shape so @nestjs/websockets does not become a required dependency.

Unit and NestJS WebSocket E2E coverage was added for unexpected errors, expected WsException values, and HTTP exceptions raised in a WebSocket context.

Root cause: WebSocket exceptions were delegated to an HTTP exception filter which expects an HTTP adapter and cannot correctly respond through a WebSocket client.

@psh4607
psh4607 marked this pull request as ready for review July 13, 2026 12:27
@psh4607
psh4607 requested a review from a team as a code owner July 13, 2026 12:27
@psh4607
psh4607 requested review from chargome, mydea and s1gr1d and removed request for a team July 13, 2026 12:27
Comment thread packages/nestjs/src/setup.ts Outdated
@psh4607
psh4607 force-pushed the feat/nestjs-ws-global-filter branch 2 times, most recently from e066783 to 6b0be86 Compare July 13, 2026 14:18
@s1gr1d
s1gr1d requested a review from nicohrubec July 14, 2026 09:28
@chargome chargome self-assigned this Jul 15, 2026

@chargome chargome left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good overall, just some minor nits.

Comment thread dev-packages/e2e-tests/test-applications/nestjs-websockets/tests/errors.test.ts Outdated
Comment thread packages/nestjs/src/setup.ts Outdated

@nicohrubec nicohrubec left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

did you run the e2e test locally? the reason I closed the original PR was that I couldn't make the e2e test pass reliably locally and it's also failing in CI now

@github-actions

Copy link
Copy Markdown
Contributor

👋 @mydea, @s1gr1d — Please review this PR when you get a chance!

@chargome

Copy link
Copy Markdown
Member

@psh4607 pinging you in case you want to push this further, otherwise we'll take over the pr next week

@psh4607
psh4607 force-pushed the feat/nestjs-ws-global-filter branch from f51a650 to 31bc079 Compare July 18, 2026 01:11
@psh4607

psh4607 commented Jul 18, 2026

Copy link
Copy Markdown
Author

Thanks for flagging this. The timeout was deterministic: Nest does not apply global APP_FILTER metadata to WebSocket gateways, so the test gateway never invoked SentryGlobalFilter. I attached the filter explicitly with @UseFilters(new SentryGlobalFilter()); the E2E suite now passes 4/4 across four local runs.

@psh4607

psh4607 commented Jul 18, 2026

Copy link
Copy Markdown
Author

@chargome Sorry for the delayed follow-up — I was away over the holiday break. This is my first contribution to sentry-javascript, so I’m excited to see it through. I’ve addressed the review comments and fixed the E2E issue. Thanks for your patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Nestjs] Support ws contexts in SentryGlobalFilter

3 participants