diff --git a/src/libraries/Common/src/System/Net/NetworkErrorHelper.cs b/src/libraries/Common/src/System/Net/NetworkErrorHelper.cs index 2e66b7f7a69ee..0baceeea91650 100644 --- a/src/libraries/Common/src/System/Net/NetworkErrorHelper.cs +++ b/src/libraries/Common/src/System/Net/NetworkErrorHelper.cs @@ -18,7 +18,8 @@ internal static NetworkException MapSocketException(SocketException socketExcept SocketError.OperationAborted => NetworkError.OperationAborted, SocketError.ConnectionAborted => NetworkError.ConnectionAborted, SocketError.ConnectionReset => NetworkError.ConnectionReset, - _ => NetworkError.Unknown + SocketError.TimedOut => NetworkError.TimedOut, + _ => NetworkError.Other }; return new NetworkException(error, socketException); diff --git a/src/libraries/System.Net.Connections/tests/System.Net.Connections.Tests/Sockets/SocketsConnectionFactoryTests.cs b/src/libraries/System.Net.Connections/tests/System.Net.Connections.Tests/Sockets/SocketsConnectionFactoryTests.cs index ce45bfb3e9ed5..9215042fe8e5d 100644 --- a/src/libraries/System.Net.Connections/tests/System.Net.Connections.Tests/Sockets/SocketsConnectionFactoryTests.cs +++ b/src/libraries/System.Net.Connections/tests/System.Net.Connections.Tests/Sockets/SocketsConnectionFactoryTests.cs @@ -134,8 +134,8 @@ public async Task ConnectAsync_TimedOut_ThrowsNetworkException() IPEndPoint doesNotExist = new IPEndPoint(IPAddress.Parse("1.2.3.4"), 23); - // SocketError.TimedOut currently maps to SocketError.Unknown, so no asserion - await Assert.ThrowsAsync(() => factory.ConnectAsync(doesNotExist).AsTask()); + NetworkException ex = await Assert.ThrowsAsync(() => factory.ConnectAsync(doesNotExist).AsTask()); + Assert.Equal(NetworkError.TimedOut, ex.NetworkError); } // On Windows, connection timeout takes 21 seconds. Abusing this behavior to test the cancellation logic diff --git a/src/libraries/System.Net.Primitives/ref/System.Net.Primitives.cs b/src/libraries/System.Net.Primitives/ref/System.Net.Primitives.cs index 9127d1bf3ca59..4abc120c3831f 100644 --- a/src/libraries/System.Net.Primitives/ref/System.Net.Primitives.cs +++ b/src/libraries/System.Net.Primitives/ref/System.Net.Primitives.cs @@ -324,9 +324,10 @@ protected TransportContext() { } } public enum NetworkError : int { - Unknown = 0, + Other = 0, EndPointInUse, HostNotFound, + TimedOut, ConnectionRefused, OperationAborted, ConnectionAborted, diff --git a/src/libraries/System.Net.Primitives/src/Resources/Strings.resx b/src/libraries/System.Net.Primitives/src/Resources/Strings.resx index d0dbe85701a4c..71c69951284b6 100644 --- a/src/libraries/System.Net.Primitives/src/Resources/Strings.resx +++ b/src/libraries/System.Net.Primitives/src/Resources/Strings.resx @@ -114,8 +114,8 @@ An invalid IPEndPoint was specified. - - An unknown network error occurred. + + A network error has occured, see InnerException for more details. The requested EndPoint is already in use. @@ -135,4 +135,7 @@ The connection was forcibly closed by the remote host. + + The connection attempt has timed out. + diff --git a/src/libraries/System.Net.Primitives/src/System/Net/NetworkError.cs b/src/libraries/System.Net.Primitives/src/System/Net/NetworkError.cs index 44bcac8678a28..5d1099a3c044e 100644 --- a/src/libraries/System.Net.Primitives/src/System/Net/NetworkError.cs +++ b/src/libraries/System.Net.Primitives/src/System/Net/NetworkError.cs @@ -6,8 +6,12 @@ namespace System.Net /// Defines a set of error codes for use with . public enum NetworkError : int { - /// An unknown network error occurred. - Unknown = 0, + /// A network error has occurred. + /// + /// This value indicates a non-generic, implementation-specific error. + /// Details could be obtained from 's inner exception. + /// + Other = 0, /// The requested EndPoint is already in use. EndPointInUse, @@ -15,6 +19,9 @@ public enum NetworkError : int /// No such host is known. HostNotFound, + /// The connection attempt has timed out. + TimedOut, + /// No connection could be made because the remote host actively refused it. ConnectionRefused, diff --git a/src/libraries/System.Net.Primitives/src/System/Net/NetworkException.cs b/src/libraries/System.Net.Primitives/src/System/Net/NetworkException.cs index 7b1b48f937914..37855524415c3 100644 --- a/src/libraries/System.Net.Primitives/src/System/Net/NetworkException.cs +++ b/src/libraries/System.Net.Primitives/src/System/Net/NetworkException.cs @@ -41,12 +41,13 @@ public override void GetObjectData(SerializationInfo serializationInfo, Streamin private static string GetExceptionMessage(NetworkError error) => error switch { NetworkError.EndPointInUse => SR.networkerror_addressinuse, + NetworkError.TimedOut => SR.networkerror_timedout, NetworkError.HostNotFound => SR.networkerror_hostnotfound, NetworkError.ConnectionRefused => SR.networkerror_connectionrefused, NetworkError.ConnectionAborted => SR.networkerror_connectionaborted, NetworkError.ConnectionReset => SR.networkerror_connectionreset, NetworkError.OperationAborted => SR.networkerror_operationaborted, - _ => SR.networkerror_unknown + _ => SR.networkerror_other }; } } diff --git a/src/libraries/System.Net.Primitives/tests/FunctionalTests/NetworkExceptionTest.cs b/src/libraries/System.Net.Primitives/tests/FunctionalTests/NetworkExceptionTest.cs index a7f60c9de1624..2f5e361d99f73 100644 --- a/src/libraries/System.Net.Primitives/tests/FunctionalTests/NetworkExceptionTest.cs +++ b/src/libraries/System.Net.Primitives/tests/FunctionalTests/NetworkExceptionTest.cs @@ -25,7 +25,7 @@ public static void Create_InnerExceptionAndMessage_Success() const string Message = "Hello"; Exception inner = new Exception(); - NetworkException e = new NetworkException(Message, NetworkError.Unknown, inner); + NetworkException e = new NetworkException(Message, NetworkError.Other, inner); Assert.Equal(inner, e.InnerException); Assert.Equal(Message, e.Message); diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/NetworkStream.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/NetworkStream.cs index e2a3ac6fff45d..2eb6a180b7ac4 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/NetworkStream.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/NetworkStream.cs @@ -795,7 +795,7 @@ private void ThrowIfDisposed() private static NetworkException GetCustomNetworkException(string message, Exception? innerException = null) { - return new NetworkException(message, NetworkError.Unknown, innerException); + return new NetworkException(message, NetworkError.Other, innerException); } } }