-
Notifications
You must be signed in to change notification settings - Fork 5.5k
fix error mapping in HttpWebRequest to handle NetworkException #40666
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,18 +96,17 @@ internal static Exception CreateCompatibleException(Exception exception) | |
|
|
||
| private static WebExceptionStatus GetStatusFromExceptionHelper(HttpRequestException ex) | ||
| { | ||
| SocketException? socketEx = ex.InnerException as SocketException; | ||
| NetworkException? networkException = ex.InnerException as NetworkException; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any concerns about a breaking change here for other consumers of HttpClient? i.e. if WebRequest is fishing out the inner exception and expecting it to be a SocketException and now it's a NetworkException?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a minor concern, but I'm not sure there's a better option. Note that this only matters for exceptions that come during Connect, since those are the only ones that have SocketException as the inner exception today. Other exceptions are already IOExceptions. The ConnectionFactory stuff is generalizing the Connect path so that you can plug in your own transport. In general these can't throw SocketException, and that's what we invented NetworkException for. We could special case SocketConnectionFactory in SocketsHttpHandler, pull out the inner SocketException, and discard the NetworkException, but that means we'll have different exception behavior based on whether you use SocketConnectionFactory or not, which seems bad. Long-term, a better solution here would be to have distinct HttpRequestException errors like HostNotFound so that a user can inspect it directly and doesn't need to fish out the inner exception. WebRequestException already has this, as you can see here. We've discussed doing this in the past but haven't actually implemented it. |
||
|
|
||
| if (socketEx is null) | ||
| if (networkException is null) | ||
| { | ||
| return WebExceptionStatus.UnknownError; | ||
| } | ||
|
|
||
| WebExceptionStatus status; | ||
| switch (socketEx.SocketErrorCode) | ||
| switch (networkException.NetworkError) | ||
| { | ||
| case SocketError.NoData: | ||
|
geoffkizer marked this conversation as resolved.
|
||
| case SocketError.HostNotFound: | ||
| case NetworkError.HostNotFound: | ||
| status = WebExceptionStatus.NameResolutionFailure; | ||
| break; | ||
| default: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be wise to add a test for this path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, yeah. I want to get this in so people aren't seeing this in CI.