Skip to content

fix(websocket): throw on ExecutionContext.switchToRpc()#93

Merged
VikramAditya33 merged 1 commit into
FOSSFORGE:mainfrom
mvanhorn:osc/89-switchToRpc-throws
May 7, 2026
Merged

fix(websocket): throw on ExecutionContext.switchToRpc()#93
VikramAditya33 merged 1 commit into
FOSSFORGE:mainfrom
mvanhorn:osc/89-switchToRpc-throws

Conversation

@mvanhorn
Copy link
Copy Markdown
Contributor

@mvanhorn mvanhorn commented May 7, 2026

What changed

src/websocket/middleware/guards/guard-executor.ts and src/websocket/middleware/filters/exception-filter-executor.ts: replaced the switchToRpc adapter (which previously returned a fake RPC context wrapping the WebSocket client and data) with a throwing implementation that mirrors the existing switchToHttp behavior:

switchToRpc: () => {
  throw new Error('RPC context not available in WebSocket');
},

Added two unit tests — one in guard-executor.spec.ts, one in exception-filter-executor.spec.ts — that build a guard / filter calling context.switchToRpc() and assert the throw.

Closes #89

Why

Issue #89 calls out that the existing switchToRpc returns a fake context whose getContext() and getData() map WebSocket fields into RPC slots. That's semantically wrong — the execution context is never an RPC context. The expected behavior, per the NestJS reference linked in the issue (packages/core/helpers/execution-context-host.ts), is that the wrong-context switches throw. The codebase already does this for switchToHttp; this PR makes switchToRpc symmetric.

Verification

npx jest src/websocket/middleware/guards/guard-executor.spec.ts src/websocket/middleware/filters/exception-filter-executor.spec.ts runs 33/33 green, including the two new tests. The new tests use the same gateway construction and execution invocation pattern as the existing switchToWs tests in those files.

Summary by CodeRabbit

  • Bug Fixes

    • WebSocket exception and guard handlers now throw a clear error when attempting to access RPC context, preventing unsupported operations.
  • Tests

    • Added test coverage for WebSocket RPC context restrictions.

Closes FOSSFORGE#89

WebSocket guards and exception filters previously returned a fake RPC
context that mapped client/data into RPC fields. This is semantically
incorrect — we're not in an RPC context — and matches the issue
reporter's expectation that switchToRpc should throw the same way
switchToHttp already does.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 7, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ea38a092-747c-43bd-b244-8e14db7d1161

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • ✅ Review completed - (🔄 Check again to review again)
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/websocket/middleware/filters/exception-filter-executor.spec.ts`:
- Around line 146-162: The test currently relies on an assertion inside
RpcCheckFilter.catch which can be swallowed by ExceptionFilterExecutor.catch;
instead have the filter call host.switchToRpc() so it throws naturally and
assert on executor.catch itself: update RpcCheckFilter.catch to simply invoke
host.switchToRpc() (no expect inside the filter), then call await
expect(executor.catch(new Error('Test'), host)).rejects.toThrow('RPC context not
available in WebSocket'); keep references to ExceptionFilterExecutor.catch
(executor.catch), RpcCheckFilter.catch, TestGateway and createHost to locate the
code.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 717bd777-d903-4ded-bea0-c74adf5857b1

📥 Commits

Reviewing files that changed from the base of the PR and between e8c673e and 9f3b11d.

📒 Files selected for processing (4)
  • src/websocket/middleware/filters/exception-filter-executor.spec.ts
  • src/websocket/middleware/filters/exception-filter-executor.ts
  • src/websocket/middleware/guards/guard-executor.spec.ts
  • src/websocket/middleware/guards/guard-executor.ts

Comment thread src/websocket/middleware/filters/exception-filter-executor.spec.ts
@VikramAditya33
Copy link
Copy Markdown
Collaborator

@mvanhorn LGTM, Thank you

@VikramAditya33 VikramAditya33 self-requested a review May 7, 2026 17:05
@VikramAditya33 VikramAditya33 merged commit cf46601 into FOSSFORGE:main May 7, 2026
4 checks passed
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.

WebSocket ExecutionContext should throw error for switchToRpc()

2 participants