Pārlūkot izejas kodu

Added a command invocation exception

CPKreuz 1 gadu atpakaļ
vecāks
revīzija
39c3aa61ea

+ 7 - 0
src/PixiEditor/Exceptions/CommandInvocationException.cs

@@ -0,0 +1,7 @@
+namespace PixiEditor.Exceptions;
+
+public class CommandInvocationException : Exception
+{
+    public CommandInvocationException(string commandName, Exception? innerException = null) : 
+        base($"Command '{commandName}' threw an exception", innerException) { }
+}

+ 12 - 4
src/PixiEditor/Models/Commands/CommandController.cs

@@ -6,6 +6,7 @@ using System.Threading.Tasks;
 using Avalonia.Media;
 using Microsoft.Extensions.DependencyInjection;
 using Newtonsoft.Json;
+using PixiEditor.Exceptions;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Extensions.Common.Localization;
 using PixiEditor.Models.AnalyticsAPI;
@@ -376,10 +377,17 @@ internal class CommandController
         {
             Analytics.SendCommand(name, (parameter as CommandExecutionContext)?.SourceInfo);
         }
-                
-        object result = method.Invoke(instance, parameters);
-        if (result is Task task)
-            task.ContinueWith(ActionOnException, TaskContinuationOptions.OnlyOnFaulted);
+
+        try
+        {
+            object result = method.Invoke(instance, parameters);
+            if (result is Task task)
+                task.ContinueWith(ActionOnException, TaskContinuationOptions.OnlyOnFaulted);
+        }
+        catch (TargetInvocationException e)
+        {
+            throw new CommandInvocationException(name, e);
+        }
 
         return;