Skip to content

Improve WebSocket exceptions to include informative messages#129428

Open
Copilot wants to merge 2 commits into
mainfrom
copilot/improve-websocket-error-messages
Open

Improve WebSocket exceptions to include informative messages#129428
Copilot wants to merge 2 commits into
mainfrom
copilot/improve-websocket-error-messages

Conversation

Copilot AI commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Closes #129192

Adds error messages for cases like receiving a text message that's not valid UTF-8, an invalid close status code / description.

The current behavior was an exception like

System.Net.WebSockets.WebSocketException (0x80004005): An exception caused the WebSocket to enter the Aborted state. Please see the InnerException, if present, for more details.

where there'd be no inner exception to inspect.

Copilot AI and others added 2 commits June 15, 2026 14:31
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 15, 2026 15:25
@MihaZupan MihaZupan changed the title Fix WebSocketReceiveErrorMessageTests stream harness Improve WebSocket exceptions to include informative messages Jun 15, 2026
@MihaZupan MihaZupan added this to the 11.0.0 milestone Jun 15, 2026
@MihaZupan MihaZupan marked this pull request as ready for review June 15, 2026 15:29
@MihaZupan MihaZupan requested review from a team and Copilot June 15, 2026 15:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR improves diagnostic quality in System.Net.WebSockets by ensuring protocol/UTF-8 receive failures throw WebSocketException instances with informative, scenario-specific messages (instead of falling back to a generic “Aborted state” message with no inner exception).

Changes:

  • Updates ManagedWebSocket receive/close error paths to pass explicit resource-based error messages into CloseWithReceiveErrorAndThrowAsync.
  • Adds new SR string resources for invalid UTF-8 text payload, protocol violation, invalid close status code, and invalid close description UTF-8.
  • Adds/updates tests to validate the new exception messages for invalid UTF-8 and malformed close frames.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/libraries/System.Net.WebSockets/tests/WebSocketUtf8Tests.cs Asserts the new invalid-UTF8 payload exception message in existing UTF-8 validation tests.
src/libraries/System.Net.WebSockets/tests/WebSocketReceiveErrorMessageTests.cs New tests that synthesize invalid frames and validate WebSocketException.Message / InnerException.
src/libraries/System.Net.WebSockets/tests/System.Net.WebSockets.Tests.csproj Includes the new test file in the test project compilation list.
src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs Supplies explicit error messages for invalid UTF-8 payload and malformed close payload scenarios.
src/libraries/System.Net.WebSockets/src/Resources/Strings.resx Adds new localized resource strings used by the improved exceptions.

@MihaZupan MihaZupan enabled auto-merge (squash) June 15, 2026 15:37
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Web Sockets: WebSocketError.Faulted is uninformative: Add and use ProtocolError and InvalidEncodedText

4 participants