Added MySql ErrorCode to MySqlCommandException. Cleaned up code. Bumped version to 1.0.9

This commit is contained in:
MattMo 2023-02-02 08:42:29 -08:00
parent 621d9c74fb
commit 7595acb740
3 changed files with 35 additions and 17 deletions

View File

@ -17,6 +17,11 @@ namespace MontoyaTech.MySqlPlus
/// </summary> /// </summary>
public MySqlCommand Command = null; public MySqlCommand Command = null;
/// <summary>
/// The MySqlErrorCode if any for this exception. -1 means no code.
/// </summary>
public int ErrorCode = -1;
/// <summary> /// <summary>
/// Creates a new MySqlCommandException with a command. /// Creates a new MySqlCommandException with a command.
/// </summary> /// </summary>
@ -35,5 +40,18 @@ namespace MontoyaTech.MySqlPlus
{ {
this.Command = command; 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;
}
} }
} }

View File

@ -182,13 +182,13 @@ namespace MontoyaTech.MySqlPlus
innerException = innerException.InnerException; innerException = innerException.InnerException;
//Try to get the MySql error code //Try to get the MySql error code
int code = -1; int errorCode = -1;
if (ex is MySqlException) if (ex is MySqlException)
code = ((MySqlException)ex).Number; errorCode = ((MySqlException)ex).Number;
//See if we have reached our max retry. //See if we have reached our max retry.
if (i + 1 >= maxRetrys || !retry) if (i + 1 >= maxRetrys || !retry)
throw new MySqlCommandException(command, ex); throw new MySqlCommandException(command, errorCode, ex);
//See if the connection was invalid //See if the connection was invalid
if (innerException.GetType().IsAssignableFrom(typeof(System.Net.Sockets.SocketException)) || 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. //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)) if (!this.Reconnect(maxRetrys, exponentialBackoff))
throw new MySqlCommandException(command, ex); throw new MySqlCommandException(command, errorCode, ex);
} }
//See if we should retry or just throw. //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 else
{ {
@ -259,13 +259,13 @@ namespace MontoyaTech.MySqlPlus
innerException = innerException.InnerException; innerException = innerException.InnerException;
//Try to get the MySql error code if we can //Try to get the MySql error code if we can
int code = -1; int errorCode = -1;
if (ex is MySqlException) if (ex is MySqlException)
code = ((MySqlException)ex).Number; errorCode = ((MySqlException)ex).Number;
//See if we have reached our max retry. //See if we have reached our max retry.
if (i + 1 >= maxRetrys || !retry) if (i + 1 >= maxRetrys || !retry)
throw new MySqlCommandException(command, ex); throw new MySqlCommandException(command, errorCode, ex);
//See if the connection was invalid //See if the connection was invalid
if (innerException.GetType().IsAssignableFrom(typeof(System.Net.Sockets.SocketException)) || 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. //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)) if (!this.Reconnect(maxRetrys, exponentialBackoff))
throw new MySqlCommandException(command, ex); throw new MySqlCommandException(command, errorCode, ex);
} }
//See if we should retry or just throw. //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 else
{ {
@ -300,14 +300,14 @@ namespace MontoyaTech.MySqlPlus
} }
/// <summary> /// <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> /// </summary>
/// <param name="number"></param> /// <param name="errorCode"></param>
/// <returns></returns> /// <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 //List of codes here: https://www.briandunning.com/error-codes/?source=MySQL
switch (number) switch (errorCode)
{ {
case 1021: case 1021:
case 1023: case 1023:

View File

@ -7,7 +7,7 @@
<AssemblyName>MontoyaTech.MySqlPlus</AssemblyName> <AssemblyName>MontoyaTech.MySqlPlus</AssemblyName>
<RootNamespace>MontoyaTech.MySqlPlus</RootNamespace> <RootNamespace>MontoyaTech.MySqlPlus</RootNamespace>
<Title>MontoyaTech.MySqlPlus</Title> <Title>MontoyaTech.MySqlPlus</Title>
<Version>1.0.8</Version> <Version>1.0.9</Version>
<Company>MontoyaTech</Company> <Company>MontoyaTech</Company>
<Description>A simple C# library to help work with MySql.</Description> <Description>A simple C# library to help work with MySql.</Description>
<Copyright>MontoyaTech 2023</Copyright> <Copyright>MontoyaTech 2023</Copyright>