From cb45d8fd2bf24a9ba84d47618f11ffad61eff40b Mon Sep 17 00:00:00 2001 From: Trayan Zapryanov Date: Mon, 31 Aug 2020 22:01:46 +0300 Subject: [PATCH] Revert "Fix | Perform Null check for SqlErrors in SqlException (#698)" This reverts commit bddefe89a129c3b6f9ab1c6dd25c3d07c59b6595. --- .../Microsoft.Data.SqlClient/SqlException.xml | 26 ++---- .../src/Microsoft/Data/SqlClient/SqlError.cs | 83 ++++++++++++++----- .../Microsoft/Data/SqlClient/SqlException.cs | 4 +- .../src/Microsoft/Data/SqlClient/SqlError.cs | 83 ++++++++++--------- .../Microsoft/Data/SqlClient/SqlException.cs | 25 +++--- 5 files changed, 127 insertions(+), 94 deletions(-) diff --git a/doc/snippets/Microsoft.Data.SqlClient/SqlException.xml b/doc/snippets/Microsoft.Data.SqlClient/SqlException.xml index c5eafa53b1..85604fde46 100644 --- a/doc/snippets/Microsoft.Data.SqlClient/SqlException.xml +++ b/doc/snippets/Microsoft.Data.SqlClient/SqlException.xml @@ -85,9 +85,7 @@ catch (Exception ex) { For information about the warning and informational messages sent by SQL Server, see the Troubleshooting section of the SQL Server documentation. This is a wrapper for the property of the first in the property. - - If is `null`, the [`default`](https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/default-values) value for `byte` is returned. - + ## Examples @@ -163,9 +161,7 @@ catch (Exception ex) { The line numbering starts at 1; if 0 is returned, the line number is not applicable. This is a wrapper for the property of the first in the property. - - If is `null`, the [`default`](https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/default-values) value for `int` is returned. - + ## Examples @@ -194,11 +190,7 @@ catch (Exception ex) { property of the first in the property. - - If is `null`, the [`default`](https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/default-values) value for `int` is returned. - - For more information on SQL Server engine errors, see [Database Engine Events and Errors](/sql/relational-databases/errors-events/database-engine-events-and-errors). + This is a wrapper for the property of the first in the property. For more information on SQL Server engine errors, see [Database Engine Events and Errors](/sql/relational-databases/errors-events/database-engine-events-and-errors). @@ -223,9 +215,8 @@ catch (Exception ex) { property of the first in the property. - - If is `null`, the [`default`](https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/default-values) value for `string` is returned. + This is a wrapper for the property of the first in the property. + ## Examples @@ -250,8 +241,7 @@ catch (Exception ex) { ## Remarks This is a wrapper for the property of the first in the property. - - If is `null`, the [`default`](https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/default-values) value for `string` is returned. + ## Examples @@ -276,6 +266,7 @@ catch (Exception ex) { ## Remarks This is a wrapper for the property of the first in the property. + ## Examples @@ -300,8 +291,7 @@ catch (Exception ex) { ## Remarks This is a wrapper for the property of the first in the property. - - If is `null`, the [`default`](https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/default-values) value for `byte` is returned. + ## Examples diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlError.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlError.cs index 05c1dd75b5..844f36312f 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlError.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlError.cs @@ -9,23 +9,34 @@ namespace Microsoft.Data.SqlClient /// public sealed class SqlError { + private string _source = TdsEnums.SQL_PROVIDER_NAME; + private int _number; + private byte _state; + private byte _errorClass; + private string _server; + private string _message; + private string _procedure; + private int _lineNumber; + private int _win32ErrorCode; + private Exception _exception; + internal SqlError(int infoNumber, byte errorState, byte errorClass, string server, string errorMessage, string procedure, int lineNumber, uint win32ErrorCode, Exception exception = null) : this(infoNumber, errorState, errorClass, server, errorMessage, procedure, lineNumber, exception) { - Win32ErrorCode = (int)win32ErrorCode; + _win32ErrorCode = (int)win32ErrorCode; } internal SqlError(int infoNumber, byte errorState, byte errorClass, string server, string errorMessage, string procedure, int lineNumber, Exception exception = null) { - Number = infoNumber; - State = errorState; - Class = errorClass; - Server = server; - Message = errorMessage; - Procedure = procedure; - LineNumber = lineNumber; - Win32ErrorCode = 0; - Exception = exception; + _number = infoNumber; + _state = errorState; + _errorClass = errorClass; + _server = server; + _message = errorMessage; + _procedure = procedure; + _lineNumber = lineNumber; + _win32ErrorCode = 0; + _exception = exception; if (errorClass != 0) { SqlClientEventSource.Log.TraceEvent(" infoNumber={0}, errorState={1}, errorClass={2}, errorMessage='{3}', procedure='{4}', lineNumber={5}", infoNumber, (int)errorState, (int)errorClass, errorMessage, procedure ?? "None", (int)lineNumber); @@ -38,35 +49,65 @@ internal SqlError(int infoNumber, byte errorState, byte errorClass, string serve // way back to SqlException. If the user needs a call stack, they can obtain it on SqlException. public override string ToString() { - return typeof(SqlError).ToString() + ": " + Message; // since this is sealed so we can change GetType to typeof + return typeof(SqlError).ToString() + ": " + _message; // since this is sealed so we can change GetType to typeof } /// - public string Source { get; private set; } = TdsEnums.SQL_PROVIDER_NAME; + public string Source + { + get { return _source; } + } /// - public int Number { get; private set; } + public int Number + { + get { return _number; } + } /// - public byte State { get; private set; } + public byte State + { + get { return _state; } + } /// - public byte Class { get; private set; } + public byte Class + { + get { return _errorClass; } + } /// - public string Server { get; private set; } + public string Server + { + get { return _server; } + } /// - public string Message { get; private set; } + public string Message + { + get { return _message; } + } /// - public string Procedure { get; private set; } + public string Procedure + { + get { return _procedure; } + } /// - public int LineNumber { get; private set; } + public int LineNumber + { + get { return _lineNumber; } + } - internal int Win32ErrorCode { get; private set; } + internal int Win32ErrorCode + { + get { return _win32ErrorCode; } + } - internal Exception Exception { get; private set; } + internal Exception Exception + { + get { return _exception; } + } } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlException.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlException.cs index 5f4e952ec8..b19798c52f 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlException.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlException.cs @@ -89,7 +89,7 @@ public Guid ClientConnectionId /// public byte Class { - get { return Errors.Count > 0 ? Errors[0].Class : default; } + get { return Errors.Count > 0 ? this.Errors[0].Class : default; } } /// @@ -125,7 +125,7 @@ public byte State /// override public string Source { - get { return TdsEnums.SQL_PROVIDER_NAME; } + get { return Errors.Count > 0 ? Errors[0].Source : default; } } /// diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlError.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlError.cs index feec3a7438..10ac16d708 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlError.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlError.cs @@ -6,108 +6,109 @@ namespace Microsoft.Data.SqlClient { - /// + /// [Serializable] public sealed class SqlError { + // bug fix - MDAC 48965 - missing source of exception - private readonly string _source = TdsEnums.SQL_PROVIDER_NAME; - private readonly int _number; - private readonly byte _state; - private readonly byte _errorClass; - [System.Runtime.Serialization.OptionalField(VersionAdded = 2)] - private readonly string _server; - private readonly string _message; - private readonly string _procedure; - private readonly int _lineNumber; - [System.Runtime.Serialization.OptionalField(VersionAdded = 4)] - private readonly int _win32ErrorCode; + private string source = TdsEnums.SQL_PROVIDER_NAME; + private int number; + private byte state; + private byte errorClass; + [System.Runtime.Serialization.OptionalFieldAttribute(VersionAdded = 2)] + private string server; + private string message; + private string procedure; + private int lineNumber; + [System.Runtime.Serialization.OptionalFieldAttribute(VersionAdded = 4)] + private int win32ErrorCode; internal SqlError(int infoNumber, byte errorState, byte errorClass, string server, string errorMessage, string procedure, int lineNumber, uint win32ErrorCode) : this(infoNumber, errorState, errorClass, server, errorMessage, procedure, lineNumber) { - _win32ErrorCode = (int)win32ErrorCode; + this.win32ErrorCode = (int)win32ErrorCode; } internal SqlError(int infoNumber, byte errorState, byte errorClass, string server, string errorMessage, string procedure, int lineNumber) { - _number = infoNumber; - _state = errorState; - _errorClass = errorClass; - _server = server; - _message = errorMessage; - _procedure = procedure; - _lineNumber = lineNumber; + this.number = infoNumber; + this.state = errorState; + this.errorClass = errorClass; + this.server = server; + this.message = errorMessage; + this.procedure = procedure; + this.lineNumber = lineNumber; if (errorClass != 0) { SqlClientEventSource.Log.TraceEvent(" infoNumber={0}, errorState={1}, errorClass={2}, errorMessage='{3}', procedure='{4}', lineNumber={5}", infoNumber, (int)errorState, (int)errorClass, errorMessage, procedure ?? "None", (int)lineNumber); } - _win32ErrorCode = 0; + this.win32ErrorCode = 0; } - /// + /// // bug fix - MDAC #49280 - SqlError does not implement ToString(); // I did not include an exception stack because the correct exception stack is only available // on SqlException, and to obtain that the SqlError would have to have backpointers all the // way back to SqlException. If the user needs a call stack, they can obtain it on SqlException. public override string ToString() { - //return GetType().ToString() + ": " + message; - return typeof(SqlError).ToString() + ": " + _message; // since this is sealed so we can change GetType to typeof + //return this.GetType().ToString() + ": " + this.message; + return typeof(SqlError).ToString() + ": " + this.message; // since this is sealed so we can change GetType to typeof } - /// + /// // bug fix - MDAC #48965 - missing source of exception public string Source { - get { return _source; } + get { return this.source; } } - /// + /// public int Number { - get { return _number; } + get { return this.number; } } - /// + /// public byte State { - get { return _state; } + get { return this.state; } } - /// + /// public byte Class { - get { return _errorClass; } + get { return this.errorClass; } } - /// + /// public string Server { - get { return _server; } + get { return this.server; } } - /// + /// public string Message { - get { return _message; } + get { return this.message; } } - /// + /// public string Procedure { - get { return _procedure; } + get { return this.procedure; } } - /// + /// public int LineNumber { - get { return _lineNumber; } + get { return this.lineNumber; } } internal int Win32ErrorCode { - get { return _win32ErrorCode; } + get { return this.win32ErrorCode; } } } } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlException.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlException.cs index 7e4fe9f411..627861e6e5 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlException.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlException.cs @@ -99,43 +99,43 @@ private bool ShouldSerializeErrors() /// public byte Class { - get { return Errors.Count > 0 ? Errors[0].Class : default; } + get { return this.Errors[0].Class; } } /// public int LineNumber { - get { return Errors.Count > 0 ? Errors[0].LineNumber : default; } + get { return this.Errors[0].LineNumber; } } /// public int Number { - get { return Errors.Count > 0 ? Errors[0].Number : default; } + get { return this.Errors[0].Number; } } /// public string Procedure { - get { return Errors.Count > 0 ? Errors[0].Procedure : default; } + get { return this.Errors[0].Procedure; } } /// public string Server { - get { return Errors.Count > 0 ? Errors[0].Server : default; } + get { return this.Errors[0].Server; } } /// public byte State { - get { return Errors.Count > 0 ? Errors[0].State : default; } + get { return this.Errors[0].State; } } /// override public string Source { - get { return TdsEnums.SQL_PROVIDER_NAME; } + get { return this.Errors[0].Source; } } /// @@ -146,7 +146,7 @@ public override string ToString() sb.AppendFormat(SQLMessage.ExClientConnectionId(), _clientConnectionId); // Append the error number, state and class if the server provided it - if (Errors.Count > 0 && Number != 0) + if (Number != 0) { sb.AppendLine(); sb.AppendFormat(SQLMessage.ExErrorNumberStateClass(), Number, State, Class); @@ -169,12 +169,12 @@ public override string ToString() return sb.ToString(); } - internal static SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion) + static internal SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion) { return CreateException(errorCollection, serverVersion, Guid.Empty); } - internal static SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, SqlInternalConnectionTds internalConnection, Exception innerException = null) + static internal SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, SqlInternalConnectionTds internalConnection, Exception innerException = null) { Guid connectionId = (internalConnection == null) ? Guid.Empty : internalConnection._clientConnectionId; var exception = CreateException(errorCollection, serverVersion, connectionId, innerException); @@ -195,10 +195,11 @@ internal static SqlException CreateException(SqlErrorCollection errorCollection, return exception; } - internal static SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, Guid conId, Exception innerException = null) + static internal SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, Guid conId, Exception innerException = null) { Debug.Assert(null != errorCollection && errorCollection.Count > 0, "no errorCollection?"); + // concat all messages together MDAC 65533 StringBuilder message = new StringBuilder(); for (int i = 0; i < errorCollection.Count; i++) { @@ -218,7 +219,7 @@ internal static SqlException CreateException(SqlErrorCollection errorCollection, exception.Data.Add("HelpLink.ProdName", "Microsoft SQL Server"); - if (!string.IsNullOrEmpty(serverVersion)) + if (!ADP.IsEmpty(serverVersion)) { exception.Data.Add("HelpLink.ProdVer", serverVersion); }