Skip to content

Commit 42b6ac6

Browse files
committed
Use same exception type when conversion fails.
This improves compatibility with Connector/NET. Signed-off-by: Bradley Grainger <[email protected]>
1 parent a23f64f commit 42b6ac6

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace MySql.Data.Types
4+
{
5+
public class MySqlConversionException : Exception
6+
{
7+
public MySqlConversionException(string message)
8+
: base(message)
9+
{
10+
}
11+
}
12+
}

src/MySqlConnector/MySql.Data.Types/MySqlDateTime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public int Millisecond
4848
}
4949

5050
public DateTime GetDateTime() =>
51-
!IsValidDateTime ? throw new InvalidOperationException("Cannot convert to DateTime when IsValidDateTime is false.") :
51+
!IsValidDateTime ? throw new MySqlConversionException("Cannot convert MySqlDateTime to DateTime when IsValidDateTime is false.") :
5252
new DateTime(Year, Month, Day, Hour, Minute, Second, DateTimeKind.Unspecified).AddTicks(Microsecond * 10);
5353

5454
public override string ToString() => IsValidDateTime ? GetDateTime().ToString() : "0000-00-00";

tests/MySqlConnector.Tests/MySqlDateTimeTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public void GetDateTime()
5151
Assert.Equal(new DateTime(2018, 6, 9, 12, 34, 56, 123).AddTicks(4560), dt);
5252
}
5353

54+
[Fact]
55+
public void GetDateTimeForInvalidDate()
56+
{
57+
var msdt = new MySqlDateTime();
58+
Assert.False(msdt.IsValidDateTime);
59+
Assert.Throws<MySqlConversionException>(() => msdt.GetDateTime());
60+
}
61+
5462
[Fact]
5563
public void SetMicrosecond()
5664
{

0 commit comments

Comments
 (0)