Added exception handling extension method

This commit is contained in:
Thorsten Sommer 2022-07-17 12:55:32 +02:00
parent 3ef7bf1784
commit 33fe8f5dac
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -0,0 +1,27 @@
using System.Text;
using DataModel;
using Microsoft.Data.Sqlite;
namespace Processor;
public static class ExtensionsError
{
public static ProcessorResult<TResult> ToProcessorResult<TResult>(this Exception exception) where TResult : class
{
if(exception.InnerException is SqliteException sqliteException)
if (sqliteException.SqliteErrorCode is 14)
{
var blockingProcesses = SystemUtil.WhoIsLocking(Setup.DataFile);
var sb = new StringBuilder();
sb.AppendLine($"The data file is used by the {blockingProcesses.Count} other process(es) listed below:");
foreach (var blockingProcess in blockingProcesses)
sb.AppendLine($"- {blockingProcess.ProcessName} (id={blockingProcess.Id})");
return new ProcessorResult<TResult>(null, false, sb.ToString());
}
else
return new ProcessorResult<TResult>(null, false, $"A database error occurred: '{sqliteException.Message}'");
return new ProcessorResult<TResult>(null, false, $"A database error occurred: '{exception.Message}'");
}
}