Skip to content
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

[SocketException]: New Constructor with string? for SocketException #74744

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static partial class SocketExceptionFactory
{
public static SocketException CreateSocketException(SocketError errorCode, int platformError)
{
return new ExtendedSocketException(errorCode, platformError);
return new SocketException(errorCode, (uint)platformError);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,9 @@ namespace System.Net.Internals
{
internal static partial class SocketExceptionFactory
{
private sealed class ExtendedSocketException : SocketException
{
private readonly EndPoint? _endPoint;

public ExtendedSocketException(int errorCode, EndPoint? endPoint)
: base(errorCode)
{
_endPoint = endPoint;
}

public ExtendedSocketException(SocketError socketError, int platformError)
: base((int)socketError)
{
HResult = platformError;
}

public override string Message =>
(_endPoint == null) ? base.Message : base.Message + " " + _endPoint.ToString();
}

public static SocketException CreateSocketException(int socketError, EndPoint? endPoint)
{
return new ExtendedSocketException(socketError, endPoint);
return new SocketException(socketError, endPoint?.ToString());
liveans marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ public partial class SocketException : System.ComponentModel.Win32Exception
{
public SocketException() { }
public SocketException(int errorCode) { }
public SocketException(int errorCode, string? message) { }
protected SocketException(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext) { }
public override int ErrorCode { get { throw null; } }
public override string Message { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@ public SocketException(int errorCode) : this((SocketError)errorCode)
// but that's the least bad option right now.
}

/// <summary>Initializes a new instance of the <see cref='System.Net.Sockets.SocketException'/> class with the specified error code and optional message.</summary>
public SocketException(int errorCode, string? message) : this((SocketError)errorCode, message)
{
}

/// <summary>Creates a new instance of the <see cref='System.Net.Sockets.SocketException'/> class with the specified error code as SocketError.</summary>
internal SocketException(SocketError socketError) : base(GetNativeErrorForSocketError(socketError))
{
_errorCode = socketError;
}

/// <summary>Initializes a new instance of the <see cref='System.Net.Sockets.SocketException'/> class with the specified error code as SocketError and optional message.</summary>
internal SocketException(SocketError socketError, string? message) : base(GetNativeErrorForSocketError(socketError), message)
{
_errorCode = socketError;
}

public override string Message => base.Message;

public SocketError SocketErrorCode => _errorCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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;
using Xunit;

namespace System.Net.Primitives.Functional.Tests
{
public static class SocketExceptionTest
{
[Fact]
public static void Create_AllErrorCodes_Success()
{
foreach (SocketError error in Enum.GetValues(typeof(SocketError)))
{
SocketException e = new SocketException((int)error);
Assert.Equal(error, e.SocketErrorCode);
Assert.Null(e.InnerException);
Assert.NotNull(e.Message);
}
}

[Fact]
public static void Create_ExceptionWithMessage_Success()
{
const string message = "Hello World";
liveans marked this conversation as resolved.
Show resolved Hide resolved
SocketException e = new SocketException((int)SocketError.AccessDenied, message);
Assert.Equal(SocketError.AccessDenied, e.SocketErrorCode);
Assert.Null(e.InnerException);
Assert.Equal(message, e.Message);
Assert.Contains(message, e.ToString());
}

[Fact]
public static void Create_SocketConnectException_Success()
liveans marked this conversation as resolved.
Show resolved Hide resolved
{
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 55555);
liveans marked this conversation as resolved.
Show resolved Hide resolved
Assert.ThrowsAsync<SocketException>(() => socket.ConnectAsync(ep));
liveans marked this conversation as resolved.
Show resolved Hide resolved
try
{
socket.Connect(ep);
Assert.Fail("Socket Connect should throw SocketException in this case.");
}
catch(SocketException ex)
{
Assert.Equal(ep.ToString(), ex.Message);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<Compile Include="LoggingTest.cs" />
<Compile Include="RequestCachePolicyTest.cs" />
<Compile Include="CookieContainerAddTest.cs" />
<Compile Include="$(CommonTestPath)System\Diagnostics\Tracing\TestEventListener.cs"
Link="Common\System\Diagnostics\Tracing\TestEventListener.cs" />
<Compile Include="$(CommonTestPath)System\Diagnostics\Tracing\TestEventListener.cs" Link="Common\System\Diagnostics\Tracing\TestEventListener.cs" />
<Compile Include="SocketExceptionTest.cs" />
</ItemGroup>
</Project>