-
Notifications
You must be signed in to change notification settings - Fork 5.3k
add NetworkException #40344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add NetworkException #40344
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9b84cf7
add NetworkException
5b5779d
Update src/libraries/System.Net.Primitives/src/Resources/Strings.resx
geoffkizer 78b6b4d
add proper serialization logic
6b83c4e
add OperationAborted and better XML comments and error strings
298f96d
use NetworkException in SocketsHttpConnectionFactory
bffcabb
Update src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpH…
geoffkizer b3cfa89
fix casing of constant in test
563f86a
Update src/libraries/System.Net.Primitives/src/Resources/Strings.resx
geoffkizer 11fc432
Update src/libraries/System.Net.Primitives/src/Resources/Strings.resx
geoffkizer 1f8d94b
Address -> EndPoint in enum values
6d7872a
move MapSocketError to a common file and fix some review issues
2185ee5
update NetworkStream to use NetworkException
a120d07
remove SocketConnectionNetworkStream and use NetworkStream directly
7785efe
remove InvalidEndPoint
9a17607
remove factory method and update constructor arguments
db769f5
Update src/libraries/System.Net.Primitives/src/System/Net/NetworkExce…
geoffkizer c266425
add OperationAborted to GetExceptionMessage
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Net.Sockets; | ||
|
|
||
| namespace System.Net | ||
| { | ||
| internal static class NetworkErrorHelper | ||
| { | ||
| internal static NetworkException MapSocketException(SocketException socketException) | ||
| { | ||
| NetworkError error = socketException.SocketErrorCode switch | ||
| { | ||
| SocketError.AddressAlreadyInUse => NetworkError.EndPointInUse, | ||
| SocketError.HostNotFound => NetworkError.HostNotFound, | ||
| SocketError.ConnectionRefused => NetworkError.ConnectionRefused, | ||
| SocketError.OperationAborted => NetworkError.OperationAborted, | ||
| SocketError.ConnectionAborted => NetworkError.ConnectionAborted, | ||
| SocketError.ConnectionReset => NetworkError.ConnectionReset, | ||
| _ => NetworkError.Unknown | ||
| }; | ||
|
|
||
| return new NetworkException(error, socketException); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/libraries/System.Net.Primitives/src/System/Net/NetworkError.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| 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>The requested EndPoint is already in use.</summary> | ||
| EndPointInUse, | ||
|
|
||
| /// <summary>No such host is known.</summary> | ||
| HostNotFound, | ||
|
|
||
| /// <summary>No connection could be made because the remote host actively refused it.</summary> | ||
| ConnectionRefused, | ||
|
|
||
| /// <summary>The operation was aborted by the user.</summary> | ||
| OperationAborted, | ||
|
|
||
| /// <summary>The connection was aborted by the local host.</summary> | ||
| ConnectionAborted, | ||
|
|
||
| /// <summary>The connection was forcibly closed by the remote host.</summary> | ||
| ConnectionReset, | ||
| } | ||
| } | ||
52 changes: 52 additions & 0 deletions
52
src/libraries/System.Net.Primitives/src/System/Net/NetworkException.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.IO; | ||
| using System.Runtime.Serialization; | ||
|
|
||
| namespace System.Net | ||
| { | ||
| /// <summary>Provides socket exceptions to the application.</summary> | ||
| [Serializable] | ||
| public class NetworkException : IOException | ||
| { | ||
| /// <summary>Creates a new instance of the <see cref='System.Net.NetworkException'/> class with the specified error code.</summary> | ||
| public NetworkException(NetworkError error, Exception? innerException = null) | ||
| : this(GetExceptionMessage(error), error, innerException) {} | ||
|
|
||
| /// <summary>Creates a new instance of the <see cref='System.Net.NetworkException'/> class with the specified error code and message.</summary> | ||
| public NetworkException(string message, NetworkError error, Exception? innerException = null) | ||
| : base(message, innerException) | ||
| { | ||
| NetworkError = error; | ||
| } | ||
|
|
||
| /// <summary>Creates a new instance of the <see cref='System.Net.NetworkException'/> from serialized data.</summary> | ||
| protected NetworkException(SerializationInfo serializationInfo, StreamingContext streamingContext) | ||
geoffkizer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| : base(serializationInfo, streamingContext) | ||
| { | ||
| NetworkError = (NetworkError)serializationInfo.GetInt32("NetworkError"); | ||
| } | ||
|
|
||
| /// <summary>Populates the serialization data for this object.</summary> | ||
| public override void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext) | ||
| { | ||
| base.GetObjectData(serializationInfo, streamingContext); | ||
| serializationInfo.AddValue("NetworkError", (int)NetworkError); | ||
| } | ||
|
|
||
| /// <summary>Returns the specific kind of error.</summary> | ||
| public NetworkError NetworkError { get; } | ||
|
|
||
| private static string GetExceptionMessage(NetworkError error) => error switch | ||
geoffkizer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| NetworkError.EndPointInUse => SR.networkerror_addressinuse, | ||
| 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 | ||
| }; | ||
| } | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
src/libraries/System.Net.Primitives/tests/FunctionalTests/NetworkExceptionTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Xunit; | ||
|
|
||
| namespace System.Net.Primitives.Functional.Tests | ||
| { | ||
| public static class NetworkExceptionTest | ||
| { | ||
| [Fact] | ||
| public static void Create_AllErrorCodes_Success() | ||
| { | ||
| foreach (NetworkError error in Enum.GetValues(typeof(NetworkError))) | ||
| { | ||
| NetworkException e = new NetworkException(error); | ||
| Assert.Equal(error, e.NetworkError); | ||
| Assert.Null(e.InnerException); | ||
| Assert.NotNull(e.Message); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void Create_InnerExceptionAndMessage_Success() | ||
| { | ||
| const string Message = "Hello"; | ||
| Exception inner = new Exception(); | ||
|
|
||
| NetworkException e = new NetworkException(Message, NetworkError.Unknown, inner); | ||
|
|
||
| Assert.Equal(inner, e.InnerException); | ||
| Assert.Equal(Message, e.Message); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.