Skip to content

Commit c9d59d8

Browse files
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 <[email protected]>
1 parent 6cf9a93 commit c9d59d8

File tree

7 files changed

+169
-187
lines changed

7 files changed

+169
-187
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@
283283
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlEnums.cs">
284284
<Link>Microsoft\Data\SqlClient\SqlEnums.cs</Link>
285285
</Compile>
286+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlError.cs">
287+
<Link>Microsoft\Data\SqlClient\SqlError.cs</Link>
288+
</Compile>
286289
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlErrorCollection.cs">
287290
<Link>Microsoft\Data\SqlClient\SqlErrorCollection.cs</Link>
288291
</Compile>
@@ -578,7 +581,6 @@
578581
<Compile Include="Microsoft\Data\SqlClient\SqlDependencyUtils.cs" />
579582
<Compile Include="Microsoft\Data\SqlClient\SqlDependencyUtils.AppDomain.cs" />
580583
<Compile Include="Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs" />
581-
<Compile Include="Microsoft\Data\SqlClient\SqlError.cs" />
582584
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlEnclaveSession.cs">
583585
<Link>Microsoft\Data\SqlClient\SqlEnclaveSession.cs</Link>
584586
</Compile>

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

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@
378378
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlEnums.cs">
379379
<Link>Microsoft\Data\SqlClient\SqlEnums.cs</Link>
380380
</Compile>
381+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlError.cs">
382+
<Link>Microsoft\Data\SqlClient\SqlError.cs</Link>
383+
</Compile>
381384
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlErrorCollection.cs">
382385
<Link>Microsoft\Data\SqlClient\SqlErrorCollection.cs</Link>
383386
</Compile>
@@ -576,7 +579,6 @@
576579
<Compile Include="Microsoft\Data\SqlClient\SqlDataReaderSmi.cs" />
577580
<Compile Include="Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs" />
578581
<Compile Include="Microsoft\Data\SqlClient\SqlDependencyUtils.cs" />
579-
<Compile Include="Microsoft\Data\SqlClient\SqlError.cs" />
580582
<Compile Include="Microsoft\Data\SqlClient\SqlInternalConnection.cs" />
581583
<Compile Include="Microsoft\Data\SqlClient\SqlInternalConnectionSmi.cs" />
582584
<Compile Include="Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs" />

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

Lines changed: 0 additions & 113 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
7+
namespace Microsoft.Data.SqlClient
8+
{
9+
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/SqlError/*' />
10+
[Serializable]
11+
public sealed class SqlError
12+
{
13+
// bug fix - MDAC 48965 - missing source of exception
14+
private readonly string _source = TdsEnums.SQL_PROVIDER_NAME;
15+
private readonly int _number;
16+
private readonly byte _state;
17+
private readonly byte _errorClass;
18+
[System.Runtime.Serialization.OptionalField(VersionAdded = 2)]
19+
private readonly string _server;
20+
private readonly string _message;
21+
private readonly string _procedure;
22+
private readonly int _lineNumber;
23+
[System.Runtime.Serialization.OptionalField(VersionAdded = 4)]
24+
private readonly int _win32ErrorCode;
25+
[System.Runtime.Serialization.OptionalField(VersionAdded = 5)]
26+
private readonly Exception _exception;
27+
28+
internal SqlError(int infoNumber, byte errorState, byte errorClass, string server, string errorMessage, string procedure, int lineNumber, uint win32ErrorCode, Exception exception = null)
29+
: this(infoNumber, errorState, errorClass, server, errorMessage, procedure, lineNumber, exception)
30+
{
31+
_server = server;
32+
_win32ErrorCode = (int)win32ErrorCode;
33+
}
34+
35+
internal SqlError(int infoNumber, byte errorState, byte errorClass, string server, string errorMessage, string procedure, int lineNumber, Exception exception = null)
36+
{
37+
_number = infoNumber;
38+
_state = errorState;
39+
_errorClass = errorClass;
40+
_server = server;
41+
_message = errorMessage;
42+
_procedure = procedure;
43+
_lineNumber = lineNumber;
44+
_win32ErrorCode = 0;
45+
_exception = exception;
46+
if (errorClass != 0)
47+
{
48+
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);
49+
}
50+
}
51+
52+
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/ToString/*' />
53+
// bug fix - MDAC #49280 - SqlError does not implement ToString();
54+
// There is no exception stack included because the correct exception stack is only available
55+
// on SqlException, and to obtain that the SqlError would have to have backpointers all the
56+
// way back to SqlException. If the user needs a call stack, they can obtain it on SqlException.
57+
public override string ToString()
58+
{
59+
return typeof(SqlError).ToString() + ": " + Message; // since this is sealed so we can change GetType to typeof
60+
}
61+
62+
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Source/*' />
63+
// bug fix - MDAC #48965 - missing source of exception
64+
public string Source => _source;
65+
66+
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Number/*' />
67+
public int Number => _number;
68+
69+
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/State/*' />
70+
public byte State => _state;
71+
72+
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Class/*' />
73+
public byte Class => _errorClass;
74+
75+
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Server/*' />
76+
public string Server => _server;
77+
78+
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Message/*' />
79+
public string Message => _message;
80+
81+
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Procedure/*' />
82+
public string Procedure => _procedure;
83+
84+
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/LineNumber/*' />
85+
public int LineNumber => _lineNumber;
86+
87+
internal int Win32ErrorCode => _win32ErrorCode;
88+
89+
internal Exception Exception => _exception;
90+
}
91+
}

src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<Compile Include="BaseProviderAsyncTest\MockDataReader.cs" />
4747
<Compile Include="SqlCredentialTest.cs" />
4848
<Compile Include="SqlDataRecordTest.cs" />
49+
<Compile Include="SqlErrorTest.cs" />
4950
<Compile Include="SqlExceptionTest.cs" />
5051
<Compile Include="SqlFacetAttributeTest.cs" />
5152
<Compile Include="SqlNotificationRequestTest.cs" />
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.IO;
7+
using System.Reflection;
8+
using System.Runtime.Serialization.Formatters.Binary;
9+
using Xunit;
10+
11+
namespace Microsoft.Data.SqlClient.Tests
12+
{
13+
public class SqlErrorTest
14+
{
15+
private const string SQLMSF_FailoverPartnerNotSupported =
16+
"Connecting to a mirrored SQL Server instance using the MultiSubnetFailover connection option is not supported.";
17+
private const byte FATAL_ERROR_CLASS = 20;
18+
19+
#if !NET50_OR_LATER
20+
[Fact]
21+
public static void SqlErrorSerializationTest()
22+
{
23+
var formatter = new BinaryFormatter();
24+
SqlError expected = CreateError();
25+
SqlError actual = null;
26+
using (var stream = new MemoryStream())
27+
{
28+
try
29+
{
30+
formatter.Serialize(stream, expected);
31+
stream.Position = 0;
32+
actual = (SqlError)formatter.Deserialize(stream);
33+
}
34+
catch (Exception ex)
35+
{
36+
Assert.False(true, $"Unexpected Exception occurred: {ex.Message}");
37+
}
38+
}
39+
40+
Assert.Equal(expected.Message, actual.Message);
41+
Assert.Equal(expected.Number, actual.Number);
42+
Assert.Equal(expected.State, actual.State);
43+
Assert.Equal(expected.Class, actual.Class);
44+
Assert.Equal(expected.Server, actual.Server);
45+
Assert.Equal(expected.Procedure, actual.Procedure);
46+
Assert.Equal(expected.LineNumber, actual.LineNumber);
47+
Assert.Equal(expected.Source, actual.Source);
48+
}
49+
#endif
50+
51+
52+
private static SqlError CreateError()
53+
{
54+
string msg = SQLMSF_FailoverPartnerNotSupported;
55+
56+
Type sqlErrorType = typeof(SqlError);
57+
58+
// SqlError only has internal constructors, in order to instantiate this, we use reflection
59+
SqlError sqlError = (SqlError)sqlErrorType.Assembly.CreateInstance(
60+
sqlErrorType.FullName,
61+
false,
62+
BindingFlags.Instance | BindingFlags.NonPublic,
63+
null,
64+
new object[] { 100, (byte)0x00, FATAL_ERROR_CLASS, "ServerName", msg, "ProcedureName", 10, null },
65+
null,
66+
null);
67+
68+
return sqlError;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)