Bug #106242 Exception type thrown from MySqlConnection.Open changed to AggregateException
Submitted: 22 Jan 2022 2:39 Modified: 26 Jan 2022 22:39
Reporter: Bradley Grainger (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:8.0.28 OS:Windows
Assigned to: CPU Architecture:Any

[22 Jan 2022 2:39] Bradley Grainger
Description:
The exception type thrown on failure by MySqlConnection.Open has changed from MySqlException to AggregateException. This is a breaking change for clients that catch (MySqlException) and handle/retry the failure.

How to repeat:
Run the following C# code:

using var connection = new MySqlConnection("Server=invalid.example.com");
try
{
	connection.Open();
}
catch (MySqlException ex)
{
	// this catch block would be reached with MySql.Data 8.0.27
	Console.WriteLine(ex.Message); // Unable to connect to any of the specified MySQL hosts.
	Console.WriteLine((MySqlErrorCode)ex.Number); // UnableToConnectToHost
}

With MySql.Data 8.0.27, the catch block would be entered, and the user's code had access to .Message and .Number to handle the exception appropriately.

With MySql.Data 8.0.28, an AggregateException containing one InnerException (a SocketException) is thrown. Information about the MySQL-specific failure (e.g., MySqlErrorCode.UnableToConnectToHost) has been lost. Moreover, if the user did not write "catch (Exception ex)", the exception will go unhandled and may crash the application.

Suggested fix:
Revert to throwing MySqlException, just like MySql.Data 8.0.27.
[22 Jan 2022 2:39] Bradley Grainger
(fix title)
[22 Jan 2022 2:40] Bradley Grainger
The problem also occurs with:

	await connection.OpenAsync();
[22 Jan 2022 7:43] MySQL Verification Team
Hello Bradley,

Thank you for the report and test case.

regards,
Umesh
[26 Jan 2022 22:39] Christine Cole
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/NET 8.0.29 release, and here's the proposed changelog entry from the documentation team:

The exception type that was returned by MySqlConnection.Open if the call
failed was changed from MySqlException to AggregateException in the
Connector/NET 8.0.28 release. This fix restores the exception to the
original type (MySqlException).

Thank you for the bug report.
[19 Feb 2024 15:27] Murilo Murilo
This still occurs in 8.3.0 version,

if I use

try
{
    await connection.OpenAsync();
}
catch (Exception ex)
{
    Logger.LogError(ex.Message);
    throw;
}

The application crashes and I can't get the exception