Skip to content

Commit 1ea1362

Browse files
committed
Add MySqlException.ErrorCode. Fixes #830
1 parent 08a5c54 commit 1ea1362

File tree

14 files changed

+57
-35
lines changed

14 files changed

+57
-35
lines changed

src/MySqlConnector/Core/CachedProcedure.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ internal sealed class CachedProcedure
5353
catch (MySqlException ex)
5454
{
5555
Log.Warn("Session{0} failed to retrieve metadata for Schema={1} Component={2}; falling back to INFORMATION_SCHEMA. Error: {3}", connection.Session.Id, schema, component, ex.Message);
56-
if ((MySqlErrorCode) ex.Number == MySqlErrorCode.TableAccessDenied)
56+
if (ex.ErrorCode == MySqlErrorCode.TableAccessDenied)
5757
connection.Session.ProcAccessDenied = true;
5858
}
5959
}

src/MySqlConnector/Core/CommandExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static async Task<MySqlDataReader> ExecuteReaderAsync(IReadOnlyList<IMySq
6161
await connection.Session.SendAsync(payload, ioBehavior, CancellationToken.None).ConfigureAwait(false);
6262
return await MySqlDataReader.CreateAsync(commandListPosition, payloadCreator, cachedProcedures, command, behavior, ioBehavior, cancellationToken).ConfigureAwait(false);
6363
}
64-
catch (MySqlException ex) when (ex.Number == (int) MySqlErrorCode.QueryInterrupted && cancellationToken.IsCancellationRequested)
64+
catch (MySqlException ex) when (ex.ErrorCode == MySqlErrorCode.QueryInterrupted && cancellationToken.IsCancellationRequested)
6565
{
6666
Log.Warn("Session{0} query was interrupted", connection.Session.Id);
6767
throw new OperationCanceledException(cancellationToken);

src/MySqlConnector/Core/ResultSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public async Task<bool> ReadAsync(IOBehavior ioBehavior, CancellationToken cance
248248
catch (MySqlException ex)
249249
{
250250
this_.BufferState = this_.State = ResultSetState.NoMoreData;
251-
if (ex.Number == (int) MySqlErrorCode.QueryInterrupted)
251+
if (ex.ErrorCode == MySqlErrorCode.QueryInterrupted)
252252
token.ThrowIfCancellationRequested();
253253
throw;
254254
}

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ public ValueTask<PayloadData> ReceiveReplyAsync(IOBehavior ioBehavior, Cancellat
768768
catch (Exception ex)
769769
{
770770
Log.Info(ex, "Session{0} failed in ReceiveReplyAsync", m_logArguments);
771-
if ((ex as MySqlException)?.Number == (int) MySqlErrorCode.CommandTimeoutExpired)
771+
if ((ex as MySqlException)?.ErrorCode == MySqlErrorCode.CommandTimeoutExpired)
772772
HandleTimeout();
773773
task = ValueTaskExtensions.FromException<ArraySegment<byte>>(ex);
774774
}
@@ -796,7 +796,7 @@ private async ValueTask<PayloadData> ReceiveReplyAsyncAwaited(ValueTask<ArraySeg
796796
catch (Exception ex)
797797
{
798798
SetFailed(ex);
799-
if (ex is MySqlException msex && msex.Number == (int) MySqlErrorCode.CommandTimeoutExpired)
799+
if (ex is MySqlException msex && msex.ErrorCode == MySqlErrorCode.CommandTimeoutExpired)
800800
HandleTimeout();
801801
throw;
802802
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ private async ValueTask<ServerSession> CreateSessionAsync(ConnectionPool? pool,
746746
var messageSuffix = (pool?.IsEmpty ?? false) ? " All pooled connections are in use." : "";
747747
throw new MySqlException(MySqlErrorCode.UnableToConnectToHost, "Connect Timeout expired." + messageSuffix, ex);
748748
}
749-
catch (MySqlException ex) when ((timeoutSource?.IsCancellationRequested ?? false) || ((MySqlErrorCode)ex.Number == MySqlErrorCode.CommandTimeoutExpired))
749+
catch (MySqlException ex) when ((timeoutSource?.IsCancellationRequested ?? false) || (ex.ErrorCode == MySqlErrorCode.CommandTimeoutExpired))
750750
{
751751
throw new MySqlException(MySqlErrorCode.UnableToConnectToHost, "Connect Timeout expired.", ex);
752752
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void ActivateResultSet()
123123
Command!.Connection!.SetSessionFailed(m_resultSet.ReadResultSetHeaderException);
124124

125125
throw mySqlException is not null ?
126-
new MySqlException(mySqlException.Number, mySqlException.SqlState, mySqlException.Message, mySqlException) :
126+
new MySqlException(mySqlException.ErrorCode, mySqlException.SqlState, mySqlException.Message, mySqlException) :
127127
new MySqlException("Failed to read the result set.", m_resultSet.ReadResultSetHeaderException);
128128
}
129129

@@ -159,7 +159,7 @@ private async Task<int> ScanResultSetAsyncAwaited(IOBehavior ioBehavior, ResultS
159159
m_hasMoreResults = resultSet.BufferState != ResultSetState.NoMoreData;
160160
return 0;
161161
}
162-
catch (MySqlException ex) when (ex.Number == (int) MySqlErrorCode.QueryInterrupted)
162+
catch (MySqlException ex) when (ex.ErrorCode == MySqlErrorCode.QueryInterrupted)
163163
{
164164
m_hasMoreResults = false;
165165
cancellationToken.ThrowIfCancellationRequested();
@@ -530,7 +530,7 @@ internal async ValueTask DisposeAsync(IOBehavior ioBehavior, CancellationToken c
530530
{
531531
}
532532
}
533-
catch (MySqlException ex) when (ex.Number == (int) MySqlErrorCode.QueryInterrupted)
533+
catch (MySqlException ex) when (ex.ErrorCode == MySqlErrorCode.QueryInterrupted)
534534
{
535535
// ignore "Query execution was interrupted" exceptions when closing a data reader
536536
}

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,33 @@
77

88
namespace MySql.Data.MySqlClient
99
{
10+
/// <summary>
11+
/// <see cref="MySqlException"/> is thrown when MySQL Server returns an error code, or there is a
12+
/// communication error with the server.
13+
/// </summary>
1014
#if !NETSTANDARD1_3
1115
[Serializable]
1216
#endif
1317
public sealed class MySqlException : DbException
1418
{
19+
/// <summary>
20+
/// A <see cref="MySqlErrorCode"/> value identifying the kind of error. Prefer to use the <see cref="ErrorCode"/> property.
21+
/// </summary>
1522
public int Number { get; }
23+
24+
/// <summary>
25+
/// A <see cref="MySqlErrorCode"/> value identifying the kind of error.
26+
/// </summary>
27+
#if NETSTANDARD1_3
28+
public MySqlErrorCode ErrorCode { get; }
29+
#else
30+
public new MySqlErrorCode ErrorCode { get; }
31+
#endif
32+
33+
/// <summary>
34+
/// A <c>SQLSTATE</c> code identifying the kind of error.
35+
/// </summary>
36+
/// <remarks>See <a href="https://en.wikipedia.org/wiki/SQLSTATE">SQLSTATE</a> for more information.</remarks>
1637
public string? SqlState { get; }
1738

1839
#if !NETSTANDARD1_3
@@ -51,29 +72,30 @@ internal MySqlException(string message)
5172
}
5273

5374
internal MySqlException(string message, Exception? innerException)
54-
: this(0, null, message, innerException)
75+
: this(default, null, message, innerException)
5576
{
5677
}
5778

5879
internal MySqlException(MySqlErrorCode errorCode, string message)
59-
: this((int) errorCode, null, message, null)
80+
: this(errorCode, null, message, null)
6081
{
6182
}
6283

6384
internal MySqlException(MySqlErrorCode errorCode, string message, Exception? innerException)
64-
: this((int) errorCode, null, message, innerException)
85+
: this(errorCode, null, message, innerException)
6586
{
6687
}
6788

68-
internal MySqlException(int errorNumber, string sqlState, string message)
69-
: this(errorNumber, sqlState, message, null)
89+
internal MySqlException(MySqlErrorCode errorCode, string sqlState, string message)
90+
: this(errorCode, sqlState, message, null)
7091
{
7192
}
7293

73-
internal MySqlException(int errorNumber, string? sqlState, string message, Exception? innerException)
94+
internal MySqlException(MySqlErrorCode errorCode, string? sqlState, string message, Exception? innerException)
7495
: base(message, innerException)
7596
{
76-
Number = errorNumber;
97+
ErrorCode = errorCode;
98+
Number = (int) errorCode;
7799
SqlState = sqlState;
78100
}
79101

src/MySqlConnector/Protocol/Payloads/ErrorPayload.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal sealed class ErrorPayload
1313
public string State { get; }
1414
public string Message { get; }
1515

16-
public MySqlException ToException() => new MySqlException(ErrorCode, State, Message);
16+
public MySqlException ToException() => new MySqlException((MySqlErrorCode) ErrorCode, State, Message);
1717

1818
public static ErrorPayload Create(ReadOnlySpan<byte> span)
1919
{

tests/MySqlConnector.Tests/MySqlExceptionTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class MySqlExceptionTests
1010
[Fact]
1111
public void IsSerializable()
1212
{
13-
var exception = new MySqlException(1, "two", "three");
13+
var exception = new MySqlException(MySqlErrorCode.No, "two", "three");
1414
MySqlException copy;
1515

1616
using (var stream = new MemoryStream())
@@ -29,8 +29,8 @@ public void IsSerializable()
2929
[Fact]
3030
public void Data()
3131
{
32-
var exception = new MySqlException(1, "two", "three");
33-
Assert.Equal(1, exception.Data["Server Error Code"]);
32+
var exception = new MySqlException(MySqlErrorCode.No, "two", "three");
33+
Assert.Equal(1002, exception.Data["Server Error Code"]);
3434
Assert.Equal("two", exception.Data["SqlState"]);
3535
}
3636
}

tests/SideBySide/BatchTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public void ExecuteInvalidSqlBatch()
228228
}
229229
catch (MySqlException ex)
230230
{
231-
Assert.Equal(MySqlErrorCode.ParseError, (MySqlErrorCode) ex.Number);
231+
Assert.Equal(MySqlErrorCode.ParseError, ex.ErrorCode);
232232
}
233233
}
234234

0 commit comments

Comments
 (0)