diff --git a/MySqlPlus/MySqlCommandException.cs b/MySqlPlus/MySqlCommandException.cs index 8bacfc7..593babc 100644 --- a/MySqlPlus/MySqlCommandException.cs +++ b/MySqlPlus/MySqlCommandException.cs @@ -17,6 +17,11 @@ namespace MontoyaTech.MySqlPlus /// </summary> public MySqlCommand Command = null; + /// <summary> + /// The MySqlErrorCode if any for this exception. -1 means no code. + /// </summary> + public int ErrorCode = -1; + /// <summary> /// Creates a new MySqlCommandException with a command. /// </summary> @@ -35,5 +40,18 @@ namespace MontoyaTech.MySqlPlus { this.Command = command; } + + /// <summary> + /// Creates a new MySqlCommandException with a command and inner exception if any. + /// </summary> + /// <param name="command"></param> + /// <param name="errorCode"></param> + /// <param name="inner"></param> + public MySqlCommandException(MySqlCommand command, int errorCode, Exception inner) : base($"Failed to execute MySqlCommand. ErrorCode: {errorCode} Query: {command.CommandText}", inner) + { + this.Command = command; + + this.ErrorCode = errorCode; + } } } diff --git a/MySqlPlus/MySqlManagedConnection.cs b/MySqlPlus/MySqlManagedConnection.cs index b9c5e47..2907369 100644 --- a/MySqlPlus/MySqlManagedConnection.cs +++ b/MySqlPlus/MySqlManagedConnection.cs @@ -182,13 +182,13 @@ namespace MontoyaTech.MySqlPlus innerException = innerException.InnerException; //Try to get the MySql error code - int code = -1; + int errorCode = -1; if (ex is MySqlException) - code = ((MySqlException)ex).Number; + errorCode = ((MySqlException)ex).Number; //See if we have reached our max retry. if (i + 1 >= maxRetrys || !retry) - throw new MySqlCommandException(command, ex); + throw new MySqlCommandException(command, errorCode, ex); //See if the connection was invalid if (innerException.GetType().IsAssignableFrom(typeof(System.Net.Sockets.SocketException)) || @@ -200,12 +200,12 @@ namespace MontoyaTech.MySqlPlus { //Attempt to reconnect, but if this fails, it means we can't try any more since the reconnect retrys more than once. if (!this.Reconnect(maxRetrys, exponentialBackoff)) - throw new MySqlCommandException(command, ex); + throw new MySqlCommandException(command, errorCode, ex); } //See if we should retry or just throw. - else if (!ShouldRetryBasedOnMySqlErrorNum(code)) + else if (!ShouldRetry(errorCode)) { - throw new MySqlCommandException(command, ex); + throw new MySqlCommandException(command, errorCode, ex); } else { @@ -259,13 +259,13 @@ namespace MontoyaTech.MySqlPlus innerException = innerException.InnerException; //Try to get the MySql error code if we can - int code = -1; + int errorCode = -1; if (ex is MySqlException) - code = ((MySqlException)ex).Number; + errorCode = ((MySqlException)ex).Number; //See if we have reached our max retry. if (i + 1 >= maxRetrys || !retry) - throw new MySqlCommandException(command, ex); + throw new MySqlCommandException(command, errorCode, ex); //See if the connection was invalid if (innerException.GetType().IsAssignableFrom(typeof(System.Net.Sockets.SocketException)) || @@ -277,12 +277,12 @@ namespace MontoyaTech.MySqlPlus { //Attempt to reconnect, but if this fails, it means we can't try any more since the reconnect retrys more than once. if (!this.Reconnect(maxRetrys, exponentialBackoff)) - throw new MySqlCommandException(command, ex); + throw new MySqlCommandException(command, errorCode, ex); } //See if we should retry or just throw. - else if (!ShouldRetryBasedOnMySqlErrorNum(code)) + else if (!ShouldRetry(errorCode)) { - throw new MySqlCommandException(command, ex); + throw new MySqlCommandException(command, errorCode, ex); } else { @@ -300,14 +300,14 @@ namespace MontoyaTech.MySqlPlus } /// <summary> - /// Returns whether or not we should retry a query based on the MySql error number. + /// Returns whether or not we should retry a query based on the MySql error code. /// </summary> - /// <param name="number"></param> + /// <param name="errorCode"></param> /// <returns></returns> - private static bool ShouldRetryBasedOnMySqlErrorNum(int number) + private static bool ShouldRetry(int errorCode) { //List of codes here: https://www.briandunning.com/error-codes/?source=MySQL - switch (number) + switch (errorCode) { case 1021: case 1023: diff --git a/MySqlPlus/MySqlPlus.csproj b/MySqlPlus/MySqlPlus.csproj index 6f2249a..c31b3b7 100644 --- a/MySqlPlus/MySqlPlus.csproj +++ b/MySqlPlus/MySqlPlus.csproj @@ -7,7 +7,7 @@ <AssemblyName>MontoyaTech.MySqlPlus</AssemblyName> <RootNamespace>MontoyaTech.MySqlPlus</RootNamespace> <Title>MontoyaTech.MySqlPlus</Title> - <Version>1.0.8</Version> + <Version>1.0.9</Version> <Company>MontoyaTech</Company> <Description>A simple C# library to help work with MySql.</Description> <Copyright>MontoyaTech 2023</Copyright>