Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move into Shared for SqlError.cs #1322

Merged
merged 11 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,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 @@ -570,7 +573,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 @@ -366,6 +366,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 @@ -562,7 +565,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
DavoudEshtehari marked this conversation as resolved.
Show resolved Hide resolved
{
// 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 @@ -44,6 +44,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="SqlParameterTest.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[] { 0, (byte)0x00, FATAL_ERROR_CLASS, null, msg, "", 0, null },
DavoudEshtehari marked this conversation as resolved.
Show resolved Hide resolved
null,
null);

return sqlError;
}
}
}