Skip to content

Commit 18957cb

Browse files
committed
Populate MySqlException.Data. Fixes #602
1 parent 9dbe0b4 commit 18957cb

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

src/MySqlConnector/MySql.Data.MySqlClient/MySqlException.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Data.Common;
34
#if !NETSTANDARD1_3
45
using System.Runtime.Serialization;
@@ -30,6 +31,20 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
3031
}
3132
#endif
3233

34+
public override IDictionary Data
35+
{
36+
get
37+
{
38+
if (m_data == null)
39+
{
40+
m_data = base.Data;
41+
m_data["Server Error Code"] = Number;
42+
m_data["SqlState"] = SqlState;
43+
}
44+
return m_data;
45+
}
46+
}
47+
3348
internal MySqlException(string message)
3449
: this(message, null)
3550
{
@@ -56,5 +71,7 @@ internal MySqlException(int errorNumber, string sqlState, string message, Except
5671

5772
internal static MySqlException CreateForTimeout(Exception innerException) =>
5873
new MySqlException((int) MySqlErrorCode.CommandTimeoutExpired, null, "The Command Timeout expired before the operation completed.", innerException);
74+
75+
IDictionary m_data;
5976
}
6077
}

tests/MySqlConnector.Tests/MySqlExceptionTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,13 @@ public void IsSerializable()
2525
Assert.Equal(exception.SqlState, copy.SqlState);
2626
Assert.Equal(exception.Message, copy.Message);
2727
}
28+
29+
[Fact]
30+
public void Data()
31+
{
32+
var exception = new MySqlException(1, "two", "three");
33+
Assert.Equal(1, exception.Data["Server Error Code"]);
34+
Assert.Equal("two", exception.Data["SqlState"]);
35+
}
2836
}
2937
}

tests/SideBySide/ConnectAsync.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public async Task ConnectBadHost()
3434
catch (MySqlException ex)
3535
{
3636
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
37+
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Data["Server Error Code"]);
3738
}
3839
Assert.Equal(ConnectionState.Closed, connection.State);
3940
}

tests/SideBySide/ConnectSync.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void ConnectBadHost()
3232
catch (MySqlException ex)
3333
{
3434
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
35+
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Data["Server Error Code"]);
3536
}
3637
Assert.Equal(ConnectionState.Closed, connection.State);
3738
}

0 commit comments

Comments
 (0)