Skip to content

Commit b77ddb8

Browse files
committed
Align SqlException Numbers across platforms
1 parent 257e326 commit b77ddb8

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniError.netcore.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#if NET
66

77
using System;
8+
using System.ComponentModel;
9+
using System.Net.Sockets;
810

911
namespace Microsoft.Data.SqlClient.ManagedSni
1012
{
@@ -40,7 +42,23 @@ public SniError(SniProviders provider, uint sniErrorCode, Exception sniException
4042
lineNumber = 0;
4143
function = string.Empty;
4244
this.provider = provider;
43-
nativeError = nativeErrorCode;
45+
if (nativeErrorCode == 0)
46+
{
47+
nativeError = nativeErrorCode;
48+
if (sniException is SocketException socketException)
49+
{
50+
// SocketErrorCode values are cross-plat consistent in .NET (matching native Windows error codes)
51+
nativeError = (uint)socketException.SocketErrorCode;
52+
}
53+
else if (sniException is Win32Exception win32Exception)
54+
{
55+
nativeError = (uint)win32Exception.NativeErrorCode; // Replicates native SNI behavior
56+
}
57+
}
58+
else
59+
{
60+
nativeError = nativeErrorCode;
61+
}
4462
sniError = sniErrorCode;
4563
errorMessage = string.Empty;
4664
exception = sniException;

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandle.netcore.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ private static Socket Connect(string serverName, int port, TimeoutTimer timeout,
375375

376376
IEnumerable<IPAddress> ipAddresses = GetHostAddressesSortedByPreference(serverName, ipPreference);
377377

378+
SocketException lastSocketException = null;
379+
378380
foreach (IPAddress ipAddress in ipAddresses)
379381
{
380382
bool isSocketSelected = false;
@@ -426,7 +428,7 @@ private static Socket Connect(string serverName, int port, TimeoutTimer timeout,
426428
{
427429
if (timeout.IsExpired)
428430
{
429-
return null;
431+
throw new Win32Exception(258, "The operation has timed out.");
430432
}
431433

432434
int socketSelectTimeout =
@@ -471,6 +473,7 @@ private static Socket Connect(string serverName, int port, TimeoutTimer timeout,
471473
SqlClientEventSource.Log.TryAdvancedTraceEvent(
472474
"{0}.{1}{2}THIS EXCEPTION IS BEING SWALLOWED: {3}",
473475
nameof(SniTcpHandle), nameof(Connect), EventType.ERR, e);
476+
lastSocketException = e;
474477
}
475478
finally
476479
{
@@ -479,6 +482,14 @@ private static Socket Connect(string serverName, int port, TimeoutTimer timeout,
479482
}
480483
}
481484

485+
if (lastSocketException != null)
486+
{
487+
SqlClientEventSource.Log.TryAdvancedTraceEvent(
488+
"{0}.{1}{2}Last Socket Exception: {3}",
489+
nameof(SniTcpHandle), nameof(Connect), EventType.ERR, lastSocketException);
490+
throw lastSocketException;
491+
}
492+
482493
return null;
483494
}
484495
}
@@ -588,6 +599,7 @@ private static Socket ParallelConnect(IPAddress[] serverAddresses, int port, Tim
588599
{
589600
SqlClientEventSource.Log.TryAdvancedTraceEvent(
590601
"{0}.{1}{2}ParallelConnect timeout expired.", nameof(SniTcpHandle), nameof(ParallelConnect), EventType.INFO);
602+
// We will throw below after cleanup
591603
break;
592604
}
593605

@@ -654,6 +666,11 @@ private static Socket ParallelConnect(IPAddress[] serverAddresses, int port, Tim
654666

655667
if (connectedSocket == null)
656668
{
669+
if (timeout.IsExpired)
670+
{
671+
throw new Win32Exception(258, "The operation has timed out.");
672+
}
673+
657674
SqlClientEventSource.Log.TryAdvancedTraceEvent(
658675
"{0}.{1}{2}No socket connections succeeded. Last error: {3}",
659676
nameof(SniTcpHandle), nameof(ParallelConnect), EventType.ERR, lastError);

0 commit comments

Comments
 (0)