Improved error handling a bit, need to update .aspx too

This commit is contained in:
Rick Falkvinge
2019-04-26 17:17:38 +02:00
parent 1a06ea3f65
commit eddc8da7b3
4 changed files with 57 additions and 6 deletions
+1
View File
@@ -84,6 +84,7 @@
<Compile Include="Enums\RoleTypeClass.cs" />
<Compile Include="Exceptions\ConcurrencyException.cs" />
<Compile Include="Exceptions\DatabaseConcurrencyException.cs" />
<Compile Include="Exceptions\DatabaseExecuteException.cs" />
<Compile Include="Exceptions\NetworkTimeoutException.cs" />
<Compile Include="Exceptions\TryAgainException.cs" />
<Compile Include="ExtensionMethods\Bitmap.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; }
}
}
+11 -1
View File
@@ -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);
}
}
}
}
+20 -5
View File
@@ -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 };
}
}