Skip to content

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

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

feat(nestjs): Support WebSocket errors in SentryGlobalFilter#22224
psh4607 wants to merge 6 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 from 6d29b23 to b67f4e8 Compare July 13, 2026 14:03
@psh4607 psh4607 force-pushed the feat/nestjs-ws-global-filter branch 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.


expect(error.exception?.values?.[0]).toMatchObject({
type: 'Error',
value: 'This is an exception in a WebSocket handler',

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.

Suggested change
value: 'This is an exception in a WebSocket handler',
value: 'This is an exception in a WebSocket handler',
mechanism: {
handled: false,
type: 'auto.ws.nestjs.global_filter',
},

});
}

const client = host.switchToWs().getClient<{ emit: (event: string, data: unknown) => void }>();

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.

Suggested change
const client = host.switchToWs().getClient<{ emit: (event: string, data: unknown) => void }>();
const client = host.switchToWs().getClient<{ emit?: (event: string, data: unknown) => void }>();

Let's put the emit function optional here, in case a user uses a ws adapter that is not compatible to this interface.

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

2 participants