Skip to content

Commit

Permalink
Add NetworkError.TimedOut, rename Unknown to Other (#40841)
Browse files Browse the repository at this point in the history
Adds NetworkError.TimedOut,
Renames Unknown to Other assuming my proposal gets accepted, will revert if not,
Improves exception message
  • Loading branch information
antonfirsov authored Aug 17, 2020
1 parent ba7c518 commit 296c90a
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/libraries/Common/src/System/Net/NetworkErrorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NetworkException>(() => factory.ConnectAsync(doesNotExist).AsTask());
NetworkException ex = await Assert.ThrowsAsync<NetworkException>(() => 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,10 @@ protected TransportContext() { }
}
public enum NetworkError : int
{
Unknown = 0,
Other = 0,
EndPointInUse,
HostNotFound,
TimedOut,
ConnectionRefused,
OperationAborted,
ConnectionAborted,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@
<data name="bad_endpoint_string" xml:space="preserve">
<value>An invalid IPEndPoint was specified.</value>
</data>
<data name="networkerror_unknown" xml:space="preserve">
<value>An unknown network error occurred.</value>
<data name="networkerror_other" xml:space="preserve">
<value>A network error has occured, see InnerException for more details.</value>
</data>
<data name="networkerror_addressinuse" xml:space="preserve">
<value>The requested EndPoint is already in use.</value>
Expand All @@ -135,4 +135,7 @@
<data name="networkerror_connectionreset" xml:space="preserve">
<value>The connection was forcibly closed by the remote host.</value>
</data>
<data name="networkerror_timedout" xml:space="preserve">
<value>The connection attempt has timed out.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ namespace System.Net
/// <summary>Defines a set of error codes for use with <see cref='System.Net.NetworkException'/>.</summary>
public enum NetworkError : int
{
/// <summary>An unknown network error occurred.</summary>
Unknown = 0,
/// <summary>A network error has occurred.</summary>
/// <remarks>
/// This value indicates a non-generic, implementation-specific error.
/// Details could be obtained from <see cref="NetworkException"/>'s inner exception.
/// </remarks>
Other = 0,

/// <summary>The requested EndPoint is already in use.</summary>
EndPointInUse,

/// <summary>No such host is known.</summary>
HostNotFound,

/// <summary>The connection attempt has timed out.</summary>
TimedOut,

/// <summary>No connection could be made because the remote host actively refused it.</summary>
ConnectionRefused,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 296c90a

Please sign in to comment.