Skip to content

Commit

Permalink
Properly catch SocketException when calling GetHostEntry
Browse files Browse the repository at this point in the history
Using GetHostEntry instead of GetHostEntryAsync + Wait means the `SocketException` is no longer wrapped inside an `AggregateException`.

Also simplify catching the the `AggregateException` in the .NET Standard 1.6 code path by using `GetBaseException()`.
  • Loading branch information
0xced committed Sep 19, 2021
1 parent 2a299b8 commit 164c1e2
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions Ductus.FluentDocker/Extensions/CommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,33 @@ public static bool IsEmulatedNative()
}


#if NETSTANDARD1_6
public static bool IsDockerDnsAvailable()
{
try
{
#if NETSTANDARD1_6
Dns.GetHostEntryAsync("host.docker.internal").Wait();
return true;
}
catch (Exception ex) when (ex.GetBaseException() is SocketException)
{
return false;
}
}
#else
public static bool IsDockerDnsAvailable()
{
try
{
Dns.GetHostEntry("host.docker.internal");
#endif
return true;
}
catch (AggregateException ex)
when (ex.InnerExceptions.Count == 1 && ex.InnerExceptions[0] is SocketException)
catch (SocketException)
{
return false;
}
}
#endif

public static bool IsNative()
{
Expand Down

0 comments on commit 164c1e2

Please sign in to comment.