Skip to content

Commit

Permalink
Move into Shared for SqlError.cs (#1322)
Browse files Browse the repository at this point in the history
* 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
lcheunglci and DavoudEshtehari authored Jan 19, 2022
1 parent 6cf9a93 commit c9d59d8
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlEnums.cs">
<Link>Microsoft\Data\SqlClient\SqlEnums.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlError.cs">
<Link>Microsoft\Data\SqlClient\SqlError.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlErrorCollection.cs">
<Link>Microsoft\Data\SqlClient\SqlErrorCollection.cs</Link>
</Compile>
Expand Down Expand Up @@ -578,7 +581,6 @@
<Compile Include="Microsoft\Data\SqlClient\SqlDependencyUtils.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlDependencyUtils.AppDomain.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlError.cs" />
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlEnclaveSession.cs">
<Link>Microsoft\Data\SqlClient\SqlEnclaveSession.cs</Link>
</Compile>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlEnums.cs">
<Link>Microsoft\Data\SqlClient\SqlEnums.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlError.cs">
<Link>Microsoft\Data\SqlClient\SqlError.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlErrorCollection.cs">
<Link>Microsoft\Data\SqlClient\SqlErrorCollection.cs</Link>
</Compile>
Expand Down Expand Up @@ -576,7 +579,6 @@
<Compile Include="Microsoft\Data\SqlClient\SqlDataReaderSmi.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlDependencyUtils.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlError.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlInternalConnection.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlInternalConnectionSmi.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs" />
Expand Down

This file was deleted.

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<Compile Include="BaseProviderAsyncTest\MockDataReader.cs" />
<Compile Include="SqlCredentialTest.cs" />
<Compile Include="SqlDataRecordTest.cs" />
<Compile Include="SqlErrorTest.cs" />
<Compile Include="SqlExceptionTest.cs" />
<Compile Include="SqlFacetAttributeTest.cs" />
<Compile Include="SqlNotificationRequestTest.cs" />
Expand Down
71 changes: 71 additions & 0 deletions src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlErrorTest.cs
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;
}
}
}

0 comments on commit c9d59d8

Please sign in to comment.