Skip to content

Commit 1b9df10

Browse files
authored
Using SNI errors as uint as far as possible (#3017)
(Also setting myself up for failure when merging LocalDBAPI, but whatever)
1 parent 861e354 commit 1b9df10

File tree

6 files changed

+29
-32
lines changed

6 files changed

+29
-32
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/LocalDBAPI.Common.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,28 +93,29 @@ internal static string GetLocalDBMessage(int hrCode)
9393
}
9494

9595

96-
private static SqlException CreateLocalDBException(string errorMessage, string instance = null, int localDbError = 0, int sniError = 0)
96+
private static SqlException CreateLocalDBException(string errorMessage, uint sniError = 0)
9797
{
98-
Debug.Assert((localDbError == 0) || (sniError == 0), "LocalDB error and SNI error cannot be specified simultaneously");
9998
Debug.Assert(!string.IsNullOrEmpty(errorMessage), "Error message should not be null or empty");
100-
SqlErrorCollection collection = new SqlErrorCollection();
101-
102-
int errorCode = (localDbError == 0) ? sniError : localDbError;
10399

104100
if (sniError != 0)
105101
{
106102
string sniErrorMessage = SQL.GetSNIErrorMessage(sniError);
107-
errorMessage = string.Format("{0} (error: {1} - {2})",
108-
errorMessage, sniError, sniErrorMessage);
103+
errorMessage = $"{errorMessage} (error: {sniError} - {sniErrorMessage})";
109104
}
110105

111-
collection.Add(new SqlError(errorCode, 0, TdsEnums.FATAL_ERROR_CLASS, instance, errorMessage, null, 0));
112-
113-
if (localDbError != 0)
114-
collection.Add(new SqlError(errorCode, 0, TdsEnums.FATAL_ERROR_CLASS, instance, GetLocalDBMessage(localDbError), null, 0));
115-
116-
SqlException exc = SqlException.CreateException(collection, null);
117-
106+
SqlErrorCollection collection = new SqlErrorCollection
107+
{
108+
new SqlError(
109+
infoNumber: (int)sniError,
110+
errorState: 0,
111+
errorClass: TdsEnums.FATAL_ERROR_CLASS,
112+
server: null,
113+
errorMessage,
114+
procedure: null,
115+
lineNumber: 0)
116+
};
117+
118+
SqlException exc = SqlException.CreateException(collection, serverVersion: null);
118119
exc._doNotReconnect = true;
119120

120121
return exc;

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/LocalDBAPI.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private static IntPtr UserInstanceDLLHandle
3030
else
3131
{
3232
SNINativeMethodWrapper.SNIGetLastError(out SniError sniError);
33-
throw CreateLocalDBException(errorMessage: StringsHelper.GetString("LocalDB_FailedGetDLLHandle"), sniError: (int)sniError.sniError);
33+
throw CreateLocalDBException(StringsHelper.GetString("LocalDB_FailedGetDLLHandle"), sniError.sniError);
3434
}
3535
}
3636
}

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,15 +1577,15 @@ internal SqlError ProcessSNIError(TdsParserStateObject stateObj)
15771577
if (TdsParserStateObjectFactory.UseManagedSNI)
15781578
{
15791579
// SNI error. Append additional error message info if available and hasn't been included.
1580-
string sniLookupMessage = SQL.GetSNIErrorMessage((int)details.sniErrorNumber);
1580+
string sniLookupMessage = SQL.GetSNIErrorMessage(details.sniErrorNumber);
15811581
errorMessage = (string.IsNullOrEmpty(errorMessage) || errorMessage.Contains(sniLookupMessage))
15821582
? sniLookupMessage
15831583
: (sniLookupMessage + ": " + errorMessage);
15841584
}
15851585
else
15861586
{
15871587
// SNI error. Replace the entire message.
1588-
errorMessage = SQL.GetSNIErrorMessage((int)details.sniErrorNumber);
1588+
errorMessage = SQL.GetSNIErrorMessage(details.sniErrorNumber);
15891589

15901590
// If its a LocalDB error, then nativeError actually contains a LocalDB-specific error code, not a win32 error code
15911591
if (details.sniErrorNumber == SniErrors.LocalDBErrorCode)

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/LocalDBAPI.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static IntPtr UserInstanceDLLHandle
9090
{
9191
SniError sniError = new SniError();
9292
SNINativeMethodWrapper.SNIGetLastError(out sniError);
93-
throw CreateLocalDBException(errorMessage: StringsHelper.GetString("LocalDB_FailedGetDLLHandle"), sniError: (int)sniError.sniError);
93+
throw CreateLocalDBException(errorMessage: StringsHelper.GetString("LocalDB_FailedGetDLLHandle"), sniError: sniError.sniError);
9494
}
9595
}
9696
}
@@ -224,13 +224,13 @@ internal static string GetLocalDBMessage(int hrCode)
224224
}
225225

226226

227-
static SqlException CreateLocalDBException(string errorMessage, string instance = null, int localDbError = 0, int sniError = 0)
227+
static SqlException CreateLocalDBException(string errorMessage, string instance = null, int localDbError = 0, uint sniError = 0)
228228
{
229229
Debug.Assert((localDbError == 0) || (sniError == 0), "LocalDB error and SNI error cannot be specified simultaneously");
230230
Debug.Assert(!string.IsNullOrEmpty(errorMessage), "Error message should not be null or empty");
231231
SqlErrorCollection collection = new SqlErrorCollection();
232232

233-
int errorCode = (localDbError == 0) ? sniError : localDbError;
233+
int errorCode = (localDbError == 0) ? (int)sniError : localDbError;
234234

235235
if (sniError != 0)
236236
{

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,9 +1872,9 @@ internal SqlError ProcessSNIError(TdsParserStateObject stateObj)
18721872
}
18731873
else
18741874
{
1875-
// SNI error. Replace the entire message
1876-
//
1877-
errorMessage = SQL.GetSNIErrorMessage((int)sniError.sniError);
1875+
// SNI error. Replace the entire message
1876+
//
1877+
errorMessage = SQL.GetSNIErrorMessage(sniError.sniError);
18781878

18791879
// If its a LocalDB error, then nativeError actually contains a LocalDB-specific error code, not a win32 error code
18801880
if (sniError.sniError == SniErrors.LocalDBErrorCode)

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlUtil.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,22 +1421,19 @@ internal static Exception MultiSubnetFailoverWithFailoverPartner(bool serverProv
14211421

14221422
internal static Exception MultiSubnetFailoverWithMoreThan64IPs()
14231423
{
1424-
// @TODO: This can be uint
1425-
string msg = GetSNIErrorMessage((int)SniErrors.MultiSubnetFailoverWithMoreThan64IPs);
1424+
string msg = GetSNIErrorMessage(SniErrors.MultiSubnetFailoverWithMoreThan64IPs);
14261425
return ADP.InvalidOperation(msg);
14271426
}
14281427

14291428
internal static Exception MultiSubnetFailoverWithInstanceSpecified()
14301429
{
1431-
// @TODO: This can be uint
1432-
string msg = GetSNIErrorMessage((int)SniErrors.MultiSubnetFailoverWithInstanceSpecified);
1430+
string msg = GetSNIErrorMessage(SniErrors.MultiSubnetFailoverWithInstanceSpecified);
14331431
return ADP.Argument(msg);
14341432
}
14351433

14361434
internal static Exception MultiSubnetFailoverWithNonTcpProtocol()
14371435
{
1438-
// @TODO: This can be uint
1439-
string msg = GetSNIErrorMessage((int)SniErrors.MultiSubnetFailoverWithNonTcpProtocol);
1436+
string msg = GetSNIErrorMessage(SniErrors.MultiSubnetFailoverWithNonTcpProtocol);
14401437
return ADP.Argument(msg);
14411438
}
14421439

@@ -2485,10 +2482,9 @@ static internal Exception TooManyValues(string arg)
24852482
/// <summary>
24862483
/// gets a message for SNI error (sniError must be valid, non-zero error code)
24872484
/// </summary>
2488-
// @TODO: This can be uint
2489-
internal static string GetSNIErrorMessage(int sniError)
2485+
internal static string GetSNIErrorMessage(uint sniError)
24902486
{
2491-
Debug.Assert(sniError > 0 && sniError <= (int)SniErrors.MaxErrorValue, "SNI error is out of range");
2487+
Debug.Assert(sniError > 0 && sniError <= SniErrors.MaxErrorValue, "SNI error is out of range");
24922488

24932489
string errorMessageId = string.Format("SNI_ERROR_{0}", sniError);
24942490
return StringsHelper.GetResourceString(errorMessageId);

0 commit comments

Comments
 (0)