diff --git a/MySqlPlus/MySqlCommandException.cs b/MySqlPlus/MySqlCommandException.cs
new file mode 100644
index 0000000..8bacfc7
--- /dev/null
+++ b/MySqlPlus/MySqlCommandException.cs
@@ -0,0 +1,39 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using MySql.Data.MySqlClient;
+
+namespace MontoyaTech.MySqlPlus
+{
+ ///
+ /// The outline of a MySqlCommandException.
+ ///
+ public class MySqlCommandException : Exception
+ {
+ ///
+ /// The MySqlCommand that failed to execute.
+ ///
+ public MySqlCommand Command = null;
+
+ ///
+ /// Creates a new MySqlCommandException with a command.
+ ///
+ ///
+ public MySqlCommandException(MySqlCommand command) : base($"Failed to execute MySqlCommand. Query: {command.CommandText}")
+ {
+ this.Command = command;
+ }
+
+ ///
+ /// Creates a new MySqlCommandException with a command and inner exception if any.
+ ///
+ ///
+ ///
+ public MySqlCommandException(MySqlCommand command, Exception inner) : base($"Failed to execute MySqlCommand. Query: {command.CommandText}", inner)
+ {
+ this.Command = command;
+ }
+ }
+}
diff --git a/MySqlPlus/MySqlManagedConnection.cs b/MySqlPlus/MySqlManagedConnection.cs
index 04039f6..b9c5e47 100644
--- a/MySqlPlus/MySqlManagedConnection.cs
+++ b/MySqlPlus/MySqlManagedConnection.cs
@@ -156,8 +156,10 @@ namespace MontoyaTech.MySqlPlus
///
public MySqlDataReader ExecuteReader(MySqlCommand command, int maxRetrys = 5, bool exponentialBackoff = true, bool retry = true)
{
- if (string.IsNullOrWhiteSpace(command.CommandText))
- throw new ArgumentException("Given command must have CommandText set.");
+ if (command == null)
+ throw new ArgumentNullException(nameof(command));
+ else if (string.IsNullOrWhiteSpace(command.CommandText))
+ throw new ArgumentException("MySqlCommand must have CommandText set.");
int backoffSleep = 2000;
command.CommandTimeout = 60; //Time in seconds
@@ -169,6 +171,7 @@ namespace MontoyaTech.MySqlPlus
try
{
command.Connection = this.MySqlConnection;
+
return command.ExecuteReader();
}
catch (Exception ex)
@@ -185,7 +188,7 @@ namespace MontoyaTech.MySqlPlus
//See if we have reached our max retry.
if (i + 1 >= maxRetrys || !retry)
- throw;
+ throw new MySqlCommandException(command, ex);
//See if the connection was invalid
if (innerException.GetType().IsAssignableFrom(typeof(System.Net.Sockets.SocketException)) ||
@@ -197,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;
+ throw new MySqlCommandException(command, ex);
}
//See if we should retry or just throw.
else if (!ShouldRetryBasedOnMySqlErrorNum(code))
{
- throw;
+ throw new MySqlCommandException(command, ex);
}
else
{
@@ -216,7 +219,7 @@ namespace MontoyaTech.MySqlPlus
}
}
- throw new Exception($"MySql ExecuteReader failed. Max timeout reached. Query: {command.CommandText}");
+ throw new MySqlCommandException(command);
}
///
@@ -230,8 +233,10 @@ namespace MontoyaTech.MySqlPlus
///
public int ExecuteNonQuery(MySqlCommand command, int maxRetrys = 5, bool exponentialBackoff = true, bool retry = true)
{
- if (string.IsNullOrWhiteSpace(command.CommandText))
- throw new ArgumentException("Given command must have CommandText set.");
+ if (command == null)
+ throw new ArgumentNullException(nameof(command));
+ else if (string.IsNullOrWhiteSpace(command.CommandText))
+ throw new ArgumentException("MySqlCommand must have CommandText set.");
int backoffSleep = 2000;
command.CommandTimeout = 60; // time in seconds
@@ -260,7 +265,7 @@ namespace MontoyaTech.MySqlPlus
//See if we have reached our max retry.
if (i + 1 >= maxRetrys || !retry)
- throw;
+ throw new MySqlCommandException(command, ex);
//See if the connection was invalid
if (innerException.GetType().IsAssignableFrom(typeof(System.Net.Sockets.SocketException)) ||
@@ -272,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;
+ throw new MySqlCommandException(command, ex);
}
//See if we should retry or just throw.
else if (!ShouldRetryBasedOnMySqlErrorNum(code))
{
- throw;
+ throw new MySqlCommandException(command, ex);
}
else
{
@@ -291,7 +296,7 @@ namespace MontoyaTech.MySqlPlus
}
}
- throw new Exception($"MySql ExecuteNonQuery failed. Max timeout reached. Query: {command.CommandText}");
+ throw new MySqlCommandException(command);
}
///
diff --git a/MySqlPlus/MySqlPlus.csproj b/MySqlPlus/MySqlPlus.csproj
index a8893a9..6f2249a 100644
--- a/MySqlPlus/MySqlPlus.csproj
+++ b/MySqlPlus/MySqlPlus.csproj
@@ -7,7 +7,7 @@
MontoyaTech.MySqlPlus
MontoyaTech.MySqlPlus
MontoyaTech.MySqlPlus
- 1.0.7
+ 1.0.8
MontoyaTech
A simple C# library to help work with MySql.
MontoyaTech 2023