From 18de7187fc9fdc86bc58a71359deca084105a479 Mon Sep 17 00:00:00 2001 From: Karl Essinger Date: Mon, 20 Jan 2025 13:42:01 +0100 Subject: [PATCH] Added more error messages to database actions --- Database.cs | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/Database.cs b/Database.cs index 9f68dd4..ab417d8 100644 --- a/Database.cs +++ b/Database.cs @@ -37,9 +37,9 @@ public static long GetNumberOfTickets() c.Open(); return (long)countTickets.ExecuteScalar(); } - catch (Exception e) + catch (MySqlException e) { - Logger.Error("Error occured when attempting to count number of open tickets: " + e); + Logger.Error("Error occured when attempting to count number of open tickets.", e); } return -1; @@ -54,9 +54,9 @@ public static long GetNumberOfClosedTickets() c.Open(); return (long)countTickets.ExecuteScalar(); } - catch (Exception e) + catch (MySqlException e) { - Logger.Error("Error occured when attempting to count number of open tickets: " + e); + Logger.Error("Error occured when attempting to count number of open tickets.", e); } return -1; @@ -365,8 +365,9 @@ public static bool DeleteOpenTicket(uint ticketID) deletion.Prepare(); return deletion.ExecuteNonQuery() > 0; } - catch (MySqlException) + catch (MySqlException e) { + Logger.Warn("Could not delete open ticket in database.", e); return false; } } @@ -403,8 +404,9 @@ public static bool Blacklist(ulong blacklistedID, ulong staffID) cmd.Prepare(); return cmd.ExecuteNonQuery() > 0; } - catch (MySqlException) + catch (MySqlException e) { + Logger.Warn("Could not blacklist user in database.", e); return false; } } @@ -420,8 +422,9 @@ public static bool Unblacklist(ulong blacklistedID) cmd.Prepare(); return cmd.ExecuteNonQuery() > 0; } - catch (MySqlException) + catch (MySqlException e) { + Logger.Warn("Could not unblacklist user in database.", e); return false; } } @@ -438,8 +441,9 @@ public static bool AssignStaff(Ticket ticket, ulong staffID) update.Prepare(); return update.ExecuteNonQuery() > 0; } - catch (MySqlException) + catch (MySqlException e) { + Logger.Warn("Could not add staff to ticket in database.", e); return false; } } @@ -461,8 +465,9 @@ public static bool SetStaffActive(ulong staffID, bool active) update.Prepare(); return update.ExecuteNonQuery() > 0; } - catch (MySqlException) + catch (MySqlException e) { + Logger.Warn("Could not set staff member as active in database.", e); return false; } } @@ -476,7 +481,6 @@ public static List GetActiveStaff(params ulong[] ignoredUserIDs) selection.Prepare(); MySqlDataReader results = selection.ExecuteReader(); - // Check if staff exists in the database if (!results.Read()) { return new List(); @@ -501,7 +505,6 @@ public static List GetAllStaff(params ulong[] ignoredUserIDs) selection.Prepare(); MySqlDataReader results = selection.ExecuteReader(); - // Check if staff exist in the database if (!results.Read()) { return new List(); @@ -526,7 +529,6 @@ public static bool IsStaff(ulong staffID) selection.Prepare(); MySqlDataReader results = selection.ExecuteReader(); - // Check if ticket exists in the database if (!results.Read()) { return false; @@ -544,7 +546,6 @@ public static bool TryGetStaff(ulong staffID, out StaffMember staffMember) selection.Prepare(); MySqlDataReader results = selection.ExecuteReader(); - // Check if ticket exists in the database if (!results.Read()) { staffMember = null; @@ -563,7 +564,6 @@ public static List GetAllMessages() selection.Prepare(); MySqlDataReader results = selection.ExecuteReader(); - // Check if messages exist in the database if (!results.Read()) { return new List(); @@ -612,8 +612,9 @@ public static bool AddMessage(string identifier, ulong userID, string message) cmd.Prepare(); return cmd.ExecuteNonQuery() > 0; } - catch (MySqlException) + catch (MySqlException e) { + Logger.Error("Could not add message to database.", e); return false; } } @@ -629,8 +630,9 @@ public static bool RemoveMessage(string identifier) cmd.Prepare(); return cmd.ExecuteNonQuery() > 0; } - catch (MySqlException) + catch (MySqlException e) { + Logger.Error("Could not remove message from database.", e); return false; } } @@ -643,7 +645,6 @@ public static List GetAllCategories() selection.Prepare(); MySqlDataReader results = selection.ExecuteReader(); - // Check if messages exist in the database if (!results.Read()) { return new List(); @@ -668,7 +669,6 @@ public static bool TryGetCategory(ulong categoryID, out Category message) selection.Prepare(); MySqlDataReader results = selection.ExecuteReader(); - // Check if ticket exists in the database if (!results.Read()) { message = null; @@ -688,7 +688,6 @@ public static bool TryGetCategory(string name, out Category message) selection.Prepare(); MySqlDataReader results = selection.ExecuteReader(); - // Check if ticket exists in the database if (!results.Read()) { message = null; @@ -711,8 +710,9 @@ public static bool AddCategory(string name, ulong categoryID) cmd.Prepare(); return cmd.ExecuteNonQuery() > 0; } - catch (MySqlException) + catch (MySqlException e) { + Logger.Error("Could not add category to database.", e); return false; } } @@ -728,8 +728,9 @@ public static bool RemoveCategory(ulong categoryID) cmd.Prepare(); return cmd.ExecuteNonQuery() > 0; } - catch (MySqlException) + catch (MySqlException e) { + Logger.Error("Could not remove category from database.", e); return false; } } @@ -743,7 +744,6 @@ public static string GetInterviewTemplateJSON(ulong categoryID) selection.Prepare(); MySqlDataReader results = selection.ExecuteReader(); - // Return a default template if it doesn't exist. if (!results.Read()) { return null; @@ -824,8 +824,9 @@ public static bool SetInterviewTemplate(Template template) cmd.Prepare(); return cmd.ExecuteNonQuery() > 0; } - catch (MySqlException) + catch (MySqlException e) { + Logger.Error("Could not set interview template in database.", e); return false; } } @@ -870,8 +871,9 @@ public static bool SaveInterview(Interview interview) cmd.Prepare(); return cmd.ExecuteNonQuery() > 0; } - catch (MySqlException) + catch (MySqlException e) { + Logger.Error("Could not save interview to database.", e); return false; } }