Skip to content

Commit 9dbe0b4

Browse files
committed
Set exception number when unable to connect. Fixes #599
1 parent 2e9fff8 commit 9dbe0b4

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public async Task ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer
280280
lock (m_lock)
281281
m_state = State.Failed;
282282
Log.Error("Session{0} connecting failed", m_logArguments);
283-
throw new MySqlException("Unable to connect to any of the specified MySQL hosts.");
283+
throw new MySqlException((int) MySqlErrorCode.UnableToConnectToHost, null, "Unable to connect to any of the specified MySQL hosts.");
284284
}
285285

286286
var byteHandler = m_socket != null ? (IByteHandler) new SocketByteHandler(m_socket) : new StreamByteHandler(m_stream);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private async Task OpenAsync(IOBehavior? ioBehavior, CancellationToken cancellat
242242
catch (SocketException ex)
243243
{
244244
SetState(ConnectionState.Closed);
245-
throw new MySqlException("Unable to connect to any of the specified MySQL hosts.", ex);
245+
throw new MySqlException((int) MySqlErrorCode.UnableToConnectToHost, null, "Unable to connect to any of the specified MySQL hosts.", ex);
246246
}
247247

248248
#if !NETSTANDARD1_3

tests/SideBySide/ConnectAsync.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ public async Task ConnectBadHost()
2626
using (var connection = new MySqlConnection(csb.ConnectionString))
2727
{
2828
Assert.Equal(ConnectionState.Closed, connection.State);
29-
await Assert.ThrowsAsync<MySqlException>(() => connection.OpenAsync());
29+
try
30+
{
31+
await connection.OpenAsync();
32+
Assert.True(false, "Exception not thrown");
33+
}
34+
catch (MySqlException ex)
35+
{
36+
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
37+
}
3038
Assert.Equal(ConnectionState.Closed, connection.State);
3139
}
3240
}

tests/SideBySide/ConnectSync.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ public void ConnectBadHost()
2424
using (var connection = new MySqlConnection(csb.ConnectionString))
2525
{
2626
Assert.Equal(ConnectionState.Closed, connection.State);
27-
Assert.Throws<MySqlException>(() => connection.Open());
27+
try
28+
{
29+
connection.Open();
30+
Assert.True(false, "Exception not thrown");
31+
}
32+
catch (MySqlException ex)
33+
{
34+
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
35+
}
2836
Assert.Equal(ConnectionState.Closed, connection.State);
2937
}
3038
}

0 commit comments

Comments
 (0)