Skip to content

Commit

Permalink
SocketHttpHandler: include host+IP information in HttpRequestException (
Browse files Browse the repository at this point in the history
#38131)

Fix #1326 by appending host:port info to HttpRequestException's message when connection fails.

Didn't change the inner SocketException, since it would require subclassing SocketException, which would add unnecessary complexity here.
  • Loading branch information
antonfirsov authored Jul 8, 2020
1 parent 5eb4ccb commit 80e954f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static async ValueTask<Stream> ConnectAsync(string host, int port, Cance
}
catch (Exception error) when (!(error is OperationCanceledException))
{
throw CreateWrappedException(error, cancellationToken);
throw CreateWrappedException(error, host, port, cancellationToken);
}
finally
{
Expand All @@ -100,7 +100,7 @@ private static Stream Connect(string host, int port, CancellationToken cancellat
catch (Exception e)
{
socket.Dispose();
throw CreateWrappedException(e, cancellationToken);
throw CreateWrappedException(e, host, port, cancellationToken);
}

return new NetworkStream(socket, ownsSocket: true);
Expand Down Expand Up @@ -242,18 +242,18 @@ public static async ValueTask<QuicConnection> ConnectQuicAsync(string host, int

if (lastException != null)
{
throw CreateWrappedException(lastException, cancellationToken);
throw CreateWrappedException(lastException, host, port, cancellationToken);
}

// TODO: find correct exception to throw here.
throw new HttpRequestException("No host found.");
}

private static Exception CreateWrappedException(Exception error, CancellationToken cancellationToken)
private static Exception CreateWrappedException(Exception error, string host, int port, CancellationToken cancellationToken)
{
return CancellationHelper.ShouldWrapInOperationCanceledException(error, cancellationToken) ?
CancellationHelper.CreateOperationCanceledException(error, cancellationToken) :
new HttpRequestException(error.Message, error, RequestRetryType.RetryOnNextProxy);
new HttpRequestException($"{error.Message} ({host}:{port})", error, RequestRetryType.RetryOnNextProxy);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Net.Test.Common;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -206,6 +207,20 @@ public async Task GetContentAsync_ErrorStatusCode_ExpectedExceptionThrown(bool w
}
}

[Fact]
[OuterLoop("Failing connection attempts take long on windows")]
public async Task GetContentAsync_WhenCanNotConnect_ExceptionContainsHostInfo()
{
using Socket portReserver = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
portReserver.Bind(new IPEndPoint(IPAddress.Loopback, 0));
IPEndPoint ep = (IPEndPoint)portReserver.LocalEndPoint;

using var client = CreateHttpClient();

HttpRequestException ex = await Assert.ThrowsAsync<HttpRequestException>(() => client.GetStreamAsync($"http://localhost:{ep.Port}"));
Assert.Contains($"localhost:{ep.Port}", ex.Message);
}

[Fact]
public async Task GetContentAsync_NullResponse_Throws()
{
Expand Down

0 comments on commit 80e954f

Please sign in to comment.