Skip to content

Commit

Permalink
Add new exception handling logic for SocketsHttpHandler differences
Browse files Browse the repository at this point in the history
  • Loading branch information
mconnew committed Mar 26, 2018
1 parent 3bc253e commit 371f442
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@
</Content>
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


using System.Diagnostics.Contracts;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Net.Sockets;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Security.Authentication;
using System.Security.Principal;
using System.ServiceModel.Security;
using System.ServiceModel.Security.Tokens;
Expand Down Expand Up @@ -132,6 +133,24 @@ public static Exception ConvertHttpRequestException(HttpRequestException excepti
Contract.Assert(exception.InnerException != null, "InnerException must be set to be able to convert");

uint hresult = (uint)exception.InnerException.HResult;
var innerSocketException = exception.InnerException as SocketException;
if (innerSocketException != null)
{
var socketErrorCode = innerSocketException.SocketErrorCode;
switch (socketErrorCode)
{
case SocketError.HostNotFound:
return new EndpointNotFoundException(SR.Format(SR.EndpointNotFound, request.RequestUri.AbsoluteUri), exception);
default:
break;
}
}

if (exception.InnerException is AuthenticationException)
{
return new SecurityNegotiationException(SR.Format(SR.TrustFailure, request.RequestUri.Authority), exception);
}

switch (hresult)
{
// .Net Native HttpClientHandler sometimes reports an incorrect handle state when a connection is aborted, so we treat it as a connection reset error
Expand Down

0 comments on commit 371f442

Please sign in to comment.