-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move into Shared for SqlError.cs (#1322)
* Merge netfx into netcore for SqlError.cs * Move the netcore version of SqlError.cs to shared src and update references in the csprojs * Fix compiler error due to relative path change to the documentation * Add the serializable attribute to the class and change the auto properties back to fields for serializations and included a serialization test for SqlError * Fix compiler error in netfx due to missed merge * Add an ifdef to SqlErrorTest because binary serialization is obsolete in NET5 to suppress a test compiler error * Update src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlErrorTest.cs Co-authored-by: DavoudEshtehari <61173489+DavoudEshtehari@users.noreply.github.com>
- Loading branch information
1 parent
6cf9a93
commit c9d59d8
Showing
7 changed files
with
169 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 0 additions & 72 deletions
72
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlError.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 0 additions & 113 deletions
113
src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlError.cs
This file was deleted.
Oops, something went wrong.
91 changes: 91 additions & 0 deletions
91
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlError.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
|
||
namespace Microsoft.Data.SqlClient | ||
{ | ||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/SqlError/*' /> | ||
[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; | ||
[System.Runtime.Serialization.OptionalField(VersionAdded = 5)] | ||
private readonly 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) | ||
{ | ||
_server = server; | ||
_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; | ||
_errorClass = errorClass; | ||
_server = server; | ||
_message = errorMessage; | ||
_procedure = procedure; | ||
_lineNumber = lineNumber; | ||
_win32ErrorCode = 0; | ||
_exception = exception; | ||
if (errorClass != 0) | ||
{ | ||
SqlClientEventSource.Log.TryTraceEvent("SqlError.ctor | ERR | Info Number {0}, Error State {1}, Error Class {2}, Error Message '{3}', Procedure '{4}', Line Number {5}", infoNumber, (int)errorState, (int)errorClass, errorMessage, procedure ?? "None", (int)lineNumber); | ||
} | ||
} | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/ToString/*' /> | ||
// bug fix - MDAC #49280 - SqlError does not implement ToString(); | ||
// There is no exception stack included 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 typeof(SqlError).ToString() + ": " + Message; // since this is sealed so we can change GetType to typeof | ||
} | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Source/*' /> | ||
// bug fix - MDAC #48965 - missing source of exception | ||
public string Source => _source; | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Number/*' /> | ||
public int Number => _number; | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/State/*' /> | ||
public byte State => _state; | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Class/*' /> | ||
public byte Class => _errorClass; | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Server/*' /> | ||
public string Server => _server; | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Message/*' /> | ||
public string Message => _message; | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Procedure/*' /> | ||
public string Procedure => _procedure; | ||
|
||
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/LineNumber/*' /> | ||
public int LineNumber => _lineNumber; | ||
|
||
internal int Win32ErrorCode => _win32ErrorCode; | ||
|
||
internal Exception Exception => _exception; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlErrorTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.SqlClient.Tests | ||
{ | ||
public class SqlErrorTest | ||
{ | ||
private const string SQLMSF_FailoverPartnerNotSupported = | ||
"Connecting to a mirrored SQL Server instance using the MultiSubnetFailover connection option is not supported."; | ||
private const byte FATAL_ERROR_CLASS = 20; | ||
|
||
#if !NET50_OR_LATER | ||
[Fact] | ||
public static void SqlErrorSerializationTest() | ||
{ | ||
var formatter = new BinaryFormatter(); | ||
SqlError expected = CreateError(); | ||
SqlError actual = null; | ||
using (var stream = new MemoryStream()) | ||
{ | ||
try | ||
{ | ||
formatter.Serialize(stream, expected); | ||
stream.Position = 0; | ||
actual = (SqlError)formatter.Deserialize(stream); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Assert.False(true, $"Unexpected Exception occurred: {ex.Message}"); | ||
} | ||
} | ||
|
||
Assert.Equal(expected.Message, actual.Message); | ||
Assert.Equal(expected.Number, actual.Number); | ||
Assert.Equal(expected.State, actual.State); | ||
Assert.Equal(expected.Class, actual.Class); | ||
Assert.Equal(expected.Server, actual.Server); | ||
Assert.Equal(expected.Procedure, actual.Procedure); | ||
Assert.Equal(expected.LineNumber, actual.LineNumber); | ||
Assert.Equal(expected.Source, actual.Source); | ||
} | ||
#endif | ||
|
||
|
||
private static SqlError CreateError() | ||
{ | ||
string msg = SQLMSF_FailoverPartnerNotSupported; | ||
|
||
Type sqlErrorType = typeof(SqlError); | ||
|
||
// SqlError only has internal constructors, in order to instantiate this, we use reflection | ||
SqlError sqlError = (SqlError)sqlErrorType.Assembly.CreateInstance( | ||
sqlErrorType.FullName, | ||
false, | ||
BindingFlags.Instance | BindingFlags.NonPublic, | ||
null, | ||
new object[] { 100, (byte)0x00, FATAL_ERROR_CLASS, "ServerName", msg, "ProcedureName", 10, null }, | ||
null, | ||
null); | ||
|
||
return sqlError; | ||
} | ||
} | ||
} |