From 33fe8f5dac8ff5da0432c922549f1ce7296ce9e9 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 17 Jul 2022 12:55:32 +0200 Subject: [PATCH] Added exception handling extension method --- I18N Commander/Processor/ExtensionsError.cs | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 I18N Commander/Processor/ExtensionsError.cs diff --git a/I18N Commander/Processor/ExtensionsError.cs b/I18N Commander/Processor/ExtensionsError.cs new file mode 100644 index 0000000..9cec361 --- /dev/null +++ b/I18N Commander/Processor/ExtensionsError.cs @@ -0,0 +1,27 @@ +using System.Text; +using DataModel; +using Microsoft.Data.Sqlite; + +namespace Processor; + +public static class ExtensionsError +{ + public static ProcessorResult ToProcessorResult(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(null, false, sb.ToString()); + } + else + return new ProcessorResult(null, false, $"A database error occurred: '{sqliteException.Message}'"); + + return new ProcessorResult(null, false, $"A database error occurred: '{exception.Message}'"); + } +} \ No newline at end of file