From eddc8da7b30afffa9bfd1da8969ef33199b62b1f Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Fri, 26 Apr 2019 17:17:38 +0200 Subject: [PATCH] Improved error handling a bit, need to update .aspx too --- Common/Common.csproj | 1 + Common/Exceptions/DatabaseExecuteException.cs | 25 +++++++++++++++++++ Database/Database-Initialization.cs | 12 ++++++++- Site/Pages/v5/Public/Initialize.aspx.cs | 25 +++++++++++++++---- 4 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 Common/Exceptions/DatabaseExecuteException.cs diff --git a/Common/Common.csproj b/Common/Common.csproj index 7ffb593a..55c28ce3 100644 --- a/Common/Common.csproj +++ b/Common/Common.csproj @@ -84,6 +84,7 @@ + diff --git a/Common/Exceptions/DatabaseExecuteException.cs b/Common/Exceptions/DatabaseExecuteException.cs new file mode 100644 index 00000000..5c2c0980 --- /dev/null +++ b/Common/Exceptions/DatabaseExecuteException.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Swarmops.Common.Exceptions +{ + public class DatabaseExecuteException: Exception + { + public DatabaseExecuteException(string attemptedCommand, Exception innerException = null) + { + if (String.IsNullOrEmpty(attemptedCommand)) + { + throw new ArgumentNullException(attemptedCommand, "Parameter cannot be null or empty"); + } + + this.AttemptedCommand = attemptedCommand; + this.InnerException = innerException; + } + + public string AttemptedCommand { get; } + public Exception InnerException { get; } + } +} diff --git a/Database/Database-Initialization.cs b/Database/Database-Initialization.cs index 19b06ab8..5abd95cc 100644 --- a/Database/Database-Initialization.cs +++ b/Database/Database-Initialization.cs @@ -1,6 +1,8 @@ using System; using System.Data; using System.Data.Common; +using MySql.Data.MySqlClient; +using Swarmops.Common.Exceptions; namespace Swarmops.Database { @@ -35,7 +37,15 @@ namespace Swarmops.Database { DbCommand command = GetDbCommand (sql, connection); command.CommandType = CommandType.Text; - command.ExecuteNonQuery(); + + try + { + command.ExecuteNonQuery(); + } + catch (MySqlException innerException) + { + throw new DatabaseExecuteException(sql, innerException); + } } } } diff --git a/Site/Pages/v5/Public/Initialize.aspx.cs b/Site/Pages/v5/Public/Initialize.aspx.cs index 724258eb..f09b450f 100644 --- a/Site/Pages/v5/Public/Initialize.aspx.cs +++ b/Site/Pages/v5/Public/Initialize.aspx.cs @@ -15,6 +15,7 @@ using NBitcoin.Protocol; using Resources; using Swarmops.Common; using Swarmops.Common.Enums; +using Swarmops.Common.Exceptions; using Swarmops.Common.ExtensionMethods; using Swarmops.Database; using Swarmops.Logic.Cache; @@ -142,7 +143,7 @@ namespace Swarmops.Frontend.Pages.v5.Public [WebMethod] - public static bool CreateDatabaseFromRoot (string mysqlHostName, string rootPassword, string serverName, + public static AjaxCallResult CreateDatabaseFromRoot (string mysqlHostName, string rootPassword, string serverName, string ipAddress, string random) { if (!(VerifyHostName (serverName) && VerifyHostAddress (ipAddress))) @@ -180,7 +181,19 @@ namespace Swarmops.Frontend.Pages.v5.Public .Replace ("[writepass]", writePass) .Replace ("[adminpass]", adminPass).Split ('#'); - SwarmDb.GetTestDatabase (rootCredentials).ExecuteAdminCommands (initInstructions); + // TODO: Make this function throw a better exception, and catch it + try + { + SwarmDb.GetTestDatabase(rootCredentials).ExecuteAdminCommands(initInstructions); + } + catch (DatabaseExecuteException sqlException) + { + return new AjaxCallResult + { + Success = false, + DisplayMessage = sqlException.AttemptedCommand + }; + } PermissionsAnalysis permissionsResult = FirstCredentialsTest ( "Swarmops-" + random, mysqlHostName, "Swarmops-R-" + random, readPass, @@ -190,14 +203,16 @@ namespace Swarmops.Frontend.Pages.v5.Public if (!permissionsResult.AllPermissionsOk) { - throw new InvalidOperationException ("waaaaaah"); + // TODO: Return a better exccption detailing exactly what permission isn't set as required + + return new AjaxCallResult {Success = false}; } - return true; + return new AjaxCallResult {Success = true}; } catch (Exception) { - return false; + return new AjaxCallResult { Success = false }; } }