Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[API Proposal]: Expose response status code in WebSocketException for WebSocketError.NotAWebSocket case #72976

Closed
IlyaBiryukov opened this issue Jul 28, 2022 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Net
Milestone

Comments

@IlyaBiryukov
Copy link
Contributor

Background and motivation

When client web socket connection fails to switch protocols, e.g. due to 401, 403, 429, or 500 errors, the client may want to know the details of the server response status code.

API Proposal

namespace System.Net.WebSockets;

public class WebSocketException : Win32Exception
{
    public int? ResponseStatusCode { get; internal set; }
}

API Usage

try
{
    var socket = new ClientWebSocket();
    await socket.ConnectAsync(uri, cancellation);
}
catch (WebSocketException wse) when (wse.WebSocketErrorCode == WebSocketError.NotAWebSocket)
{
    Console.WriteLine($"Couldn't connect to web socket service. Status code: {wse.ResponseStatusCode}");
}

Alternative Designs

Use WebSocketException constructors with nativeError and pass in the response status code as native error. The downside is that the native error is expected to be an HResult, not an HTTP response status code.

Risks

Low risks.

@IlyaBiryukov IlyaBiryukov added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jul 28, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 28, 2022
@ghost
Copy link

ghost commented Jul 28, 2022

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

Issue Details

Background and motivation

When client web socket connection fails to switch protocols, e.g. due to 401, 403, 429, or 500 errors, the client may want to know the details of the server response status code.

API Proposal

namespace System.Net.WebSockets;

public class WebSocketException : Win32Exception
{
    public int? ResponseStatusCode { get; internal set; }
}

API Usage

try
{
    var socket = new ClientWebSocket();
    await socket.ConnectAsync(uri, cancellation);
}
catch (WebSocketException wse) when (wse.WebSocketErrorCode == WebSocketError.NotAWebSocket)
{
    Console.WriteLine($"Couldn't connect to web socket service. Status code: {wse.ResponseStatusCode}");
}

Alternative Designs

Use WebSocketException constructors with nativeError and pass in the response status code as native error. The downside is that the native error is expected to be an HResult, not an HTTP response status code.

Risks

Low risks.

Author: IlyaBiryukov
Assignees: -
Labels:

api-suggestion, area-System.Net

Milestone: -

@MihaZupan
Copy link
Member

This has been added for 7.0 as part of #25918 (comment).

You have to set ClientWebSocketOptions.CollectHttpResponseDetails = true and then look at ClientWebSocket.HttpStatusCode.

@CarnaViire
Copy link
Member

Duplicate of #25918

@CarnaViire CarnaViire marked this as a duplicate of #25918 Jul 28, 2022
@CarnaViire CarnaViire closed this as not planned Won't fix, can't repro, duplicate, stale Jul 28, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jul 28, 2022
@karelz karelz added this to the 7.0.0 milestone Aug 7, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Net
Projects
None yet
Development

No branches or pull requests

4 participants