Skip to content

Commit 88dd85a

Browse files
Fix missing error messages in Managed SNI
1 parent f0c9fa6 commit 88dd85a

7 files changed

Lines changed: 41 additions & 23 deletions

File tree

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ internal static uint MapLocalDBErrorStateToCode(LocalDBErrorState errorState)
8888
}
8989
}
9090

91+
internal static string MapLocalDBErrorStateToErrorMessage(LocalDBErrorState errorState)
92+
{
93+
switch (errorState)
94+
{
95+
case LocalDBErrorState.NO_INSTALLATION:
96+
return Strings.SNI_ERROR_52;
97+
case LocalDBErrorState.INVALID_CONFIG:
98+
return Strings.SNI_ERROR_53;
99+
case LocalDBErrorState.NO_SQLUSERINSTANCEDLL_PATH:
100+
return Strings.SNI_ERROR_54;
101+
case LocalDBErrorState.INVALID_SQLUSERINSTANCEDLL_PATH:
102+
return Strings.SNI_ERROR_55;
103+
case LocalDBErrorState.NONE:
104+
return Strings.SNI_ERROR_50;
105+
default:
106+
return Strings.SNI_ERROR_53;
107+
}
108+
}
109+
91110
/// <summary>
92111
/// Loads the User Instance dll.
93112
/// </summary>
@@ -117,15 +136,15 @@ private bool LoadUserInstanceDll()
117136
// If there was no DLL path found, then there is an error.
118137
if (dllPath == null)
119138
{
120-
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, MapLocalDBErrorStateToCode(registryQueryErrorState), string.Empty);
139+
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, MapLocalDBErrorStateToCode(registryQueryErrorState), MapLocalDBErrorStateToErrorMessage(registryQueryErrorState));
121140
SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.LocalDB.Windows.LoadUserInstanceDll |SNI|ERR >User instance DLL path is null.");
122141
return false;
123142
}
124143

125144
// In case the registry had an empty path for dll
126145
if (string.IsNullOrWhiteSpace(dllPath))
127146
{
128-
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBInvalidSqlUserInstanceDllPath, string.Empty);
147+
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBInvalidSqlUserInstanceDllPath, Strings.SNI_ERROR_55);
129148
SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.LocalDB.Windows.LoadUserInstanceDll |SNI|ERR > User instance DLL path is invalid. DLL path ={0}", dllPath);
130149
return false;
131150
}
@@ -135,7 +154,7 @@ private bool LoadUserInstanceDll()
135154

136155
if (libraryHandle.IsInvalid)
137156
{
138-
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBFailedToLoadDll, string.Empty);
157+
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBFailedToLoadDll, Strings.SNI_ERROR_56);
139158
SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.LocalDB.Windows.LoadUserInstanceDll |SNI|ERR > Library Handle is invalid. Could not load the dll.");
140159
libraryHandle.Dispose();
141160
return false;
@@ -146,7 +165,7 @@ private bool LoadUserInstanceDll()
146165

147166
if (_startInstanceHandle == IntPtr.Zero)
148167
{
149-
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBBadRuntime, string.Empty);
168+
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBBadRuntime, Strings.SNI_ERROR_57);
150169
SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.LocalDB.Windows.LoadUserInstanceDll |SNI|ERR > Was not able to load the PROC from DLL. Bad Runtime.");
151170
libraryHandle.Dispose();
152171
return false;
@@ -157,7 +176,7 @@ private bool LoadUserInstanceDll()
157176

158177
if (localDBStartInstanceFunc == null)
159178
{
160-
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBBadRuntime, string.Empty);
179+
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBBadRuntime, Strings.SNI_ERROR_57);
161180
libraryHandle.Dispose();
162181
_startInstanceHandle = IntPtr.Zero;
163182
return false;

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public uint StartReceive()
7575
return TdsEnums.SNI_SUCCESS_IO_PENDING;
7676
}
7777
SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.SNIMarsConnection.StartReceive |SNI|ERR> Connection not useable.");
78-
return SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, 0, SNICommon.ConnNotUsableError, string.Empty);
78+
return SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, 0, SNICommon.ConnNotUsableError, Strings.SNI_ERROR_19);
7979
}
8080
finally
8181
{
@@ -293,7 +293,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode)
293293

294294
if (!_sessions.ContainsKey(_currentHeader.sessionId))
295295
{
296-
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.SMUX_PROV, 0, SNICommon.InvalidParameterError, string.Empty);
296+
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.SMUX_PROV, 0, SNICommon.InvalidParameterError, Strings.SNI_ERROR_5);
297297
HandleReceiveError(packet);
298298
_lowerHandle.Dispose();
299299
_lowerHandle = null;

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
530530

531531
if (!_packetEvent.Wait(timeoutInMilliseconds))
532532
{
533-
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.SMUX_PROV, 0, SNICommon.ConnTimeoutError, string.Empty);
533+
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.SMUX_PROV, 0, SNICommon.ConnTimeoutError, Strings.SNI_ERROR_11);
534534
return TdsEnums.SNI_WAIT_TIMEOUT;
535535
}
536536
}

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ internal sealed class SNINpHandle : SNIPhysicalHandle
2222
private const int MAX_PIPE_INSTANCES = 255;
2323

2424
private readonly string _targetServer;
25-
private readonly object _callbackObject;
2625
private readonly object _sendSync;
2726

2827
private Stream _stream;
@@ -38,15 +37,14 @@ internal sealed class SNINpHandle : SNIPhysicalHandle
3837
private int _bufferSize = TdsEnums.DEFAULT_LOGIN_PACKET_SIZE;
3938
private readonly Guid _connectionId = Guid.NewGuid();
4039

41-
public SNINpHandle(string serverName, string pipeName, long timerExpire, object callbackObject)
40+
public SNINpHandle(string serverName, string pipeName, long timerExpire)
4241
{
4342
long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("<sc.SNI.SNINpHandle.SNINpHandle |SNI|INFO|SCOPE> Constructor");
4443
SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.SNINpHandle.SNINpHandle |SNI|INFO> Constructor. server name = {0}, pipe name = {1}", serverName, pipeName);
4544
try
4645
{
4746
_sendSync = new object();
4847
_targetServer = serverName;
49-
_callbackObject = callbackObject;
5048

5149
try
5250
{
@@ -86,7 +84,7 @@ public SNINpHandle(string serverName, string pipeName, long timerExpire, object
8684

8785
if (!_pipeStream.IsConnected || !_pipeStream.CanWrite || !_pipeStream.CanRead)
8886
{
89-
SNICommon.ReportSNIError(SNIProviders.NP_PROV, 0, SNICommon.ConnOpenFailedError, string.Empty);
87+
SNICommon.ReportSNIError(SNIProviders.NP_PROV, 0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_1);
9088
_status = TdsEnums.SNI_ERROR;
9189
SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.SNINpHandle.SNINpHandle |SNI|ERR> Pipe stream is not connected or cannot write or read to/from it.");
9290
return;
@@ -126,7 +124,7 @@ public override int ProtocolVersion
126124
{
127125
try
128126
{
129-
return (int)_sslStream.SslProtocol;
127+
return (int)_sslStream?.SslProtocol;
130128
}
131129
catch
132130
{

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ private void ReadFromStreamAsyncContinuation(Task<int> t, object state)
278278

279279
if (_dataLength == 0)
280280
{
281-
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, SNICommon.ConnTerminatedError, string.Empty);
281+
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, SNICommon.ConnTerminatedError, Strings.SNI_ERROR_2);
282282
error = true;
283283
}
284284
}

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ private SNITCPHandle CreateTcpHandle(DataSource details, long timerExpire, objec
379379
string hostName = details.ServerName;
380380
if (string.IsNullOrWhiteSpace(hostName))
381381
{
382-
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, SNICommon.InvalidConnStringError, string.Empty);
382+
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, SNICommon.InvalidConnStringError, Strings.SNI_ERROR_25);
383383
return null;
384384
}
385385

@@ -425,10 +425,11 @@ private SNINpHandle CreateNpHandle(DataSource details, long timerExpire, object
425425
{
426426
if (parallel)
427427
{
428-
SNICommon.ReportSNIError(SNIProviders.NP_PROV, 0, SNICommon.MultiSubnetFailoverWithNonTcpProtocol, string.Empty);
428+
// Connecting to a SQL Server instance using the MultiSubnetFailover connection option is only supported when using the TCP protocol
429+
SNICommon.ReportSNIError(SNIProviders.NP_PROV, 0, SNICommon.MultiSubnetFailoverWithNonTcpProtocol, Strings.SNI_ERROR_49);
429430
return null;
430431
}
431-
return new SNINpHandle(details.PipeHostName, details.PipeName, timerExpire, callbackObject);
432+
return new SNINpHandle(details.PipeHostName, details.PipeName, timerExpire);
432433
}
433434

434435
/// <summary>
@@ -632,7 +633,7 @@ internal static string GetLocalDBInstance(string dataSource, out bool error)
632633
}
633634
else
634635
{
635-
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBNoInstanceName, string.Empty);
636+
SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBNoInstanceName, Strings.SNI_ERROR_51);
636637
error = true;
637638
return null;
638639
}
@@ -758,7 +759,7 @@ private bool InferConnectionDetails()
758759

759760
private void ReportSNIError(SNIProviders provider)
760761
{
761-
SNILoadHandle.SingletonInstance.LastError = new SNIError(provider, 0, SNICommon.InvalidConnStringError, string.Empty);
762+
SNILoadHandle.SingletonInstance.LastError = new SNIError(provider, 0, SNICommon.InvalidConnStringError, Strings.SNI_ERROR_25);
762763
IsBadDataSource = true;
763764
}
764765

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public SNITCPHandle(string serverName, int port, long timerExpire, object callba
217217

218218
if (reportError)
219219
{
220-
ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, string.Empty);
220+
ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_40);
221221
}
222222
return;
223223
}
@@ -259,7 +259,7 @@ private Socket TryConnectParallel(string hostName, int port, TimeSpan ts, bool i
259259
{
260260
// Fail if above 64 to match legacy behavior
261261
callerReportError = false;
262-
ReportTcpSNIError(0, SNICommon.MultiSubnetFailoverWithMoreThan64IPs, string.Empty);
262+
ReportTcpSNIError(0, SNICommon.MultiSubnetFailoverWithMoreThan64IPs, Strings.SNI_ERROR_47);
263263
return availableSocket;
264264
}
265265

@@ -288,7 +288,7 @@ private Socket TryConnectParallel(string hostName, int port, TimeSpan ts, bool i
288288
if (!(isInfiniteTimeOut ? connectTask.Wait(-1) : connectTask.Wait(ts)))
289289
{
290290
callerReportError = false;
291-
ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, string.Empty);
291+
ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_40);
292292
return availableSocket;
293293
}
294294

@@ -644,7 +644,7 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
644644
else
645645
{
646646
// otherwise it is timeout for 0 or less than -1
647-
ReportTcpSNIError(0, SNICommon.ConnTimeoutError, string.Empty);
647+
ReportTcpSNIError(0, SNICommon.ConnTimeoutError, Strings.SNI_ERROR_11);
648648
return TdsEnums.SNI_WAIT_TIMEOUT;
649649
}
650650

0 commit comments

Comments
 (0)