From 2fcc6d786241c4eb5d2c658da047cd1b50646eee Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 8 Apr 2023 13:13:58 +0200 Subject: [PATCH] Plugins - Added constructors to PluginException to provide a help document --- .../Exceptions/ArtemisPluginException.cs | 28 +++++++++++++++++-- .../Interfaces/IPluginManagementService.cs | 7 +++++ .../Services/PluginManagementService.cs | 14 +++++++++- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/Artemis.Core/Exceptions/ArtemisPluginException.cs b/src/Artemis.Core/Exceptions/ArtemisPluginException.cs index 8e49b0e19..53f9fd30f 100644 --- a/src/Artemis.Core/Exceptions/ArtemisPluginException.cs +++ b/src/Artemis.Core/Exceptions/ArtemisPluginException.cs @@ -10,7 +10,7 @@ public class ArtemisPluginException : Exception /// /// Creates a new instance of the class /// - public ArtemisPluginException(Plugin plugin) + internal ArtemisPluginException(Plugin plugin) { Plugin = plugin; } @@ -18,7 +18,7 @@ public class ArtemisPluginException : Exception /// /// Creates a new instance of the class /// - public ArtemisPluginException(Plugin plugin, string message) : base(message) + internal ArtemisPluginException(Plugin plugin, string message) : base(message) { Plugin = plugin; } @@ -26,7 +26,7 @@ public class ArtemisPluginException : Exception /// /// Creates a new instance of the class /// - public ArtemisPluginException(Plugin plugin, string message, Exception inner) : base(message, inner) + internal ArtemisPluginException(Plugin plugin, string message, Exception inner) : base(message, inner) { Plugin = plugin; } @@ -44,9 +44,31 @@ public class ArtemisPluginException : Exception public ArtemisPluginException(string message, Exception inner) : base(message, inner) { } + + /// + /// Creates a new instance of the class + /// + public ArtemisPluginException(string message, string helpDocument) : base(message) + { + HelpDocument = helpDocument; + } + + /// + /// Creates a new instance of the class + /// + public ArtemisPluginException(string message, Exception inner, string helpDocument) : base(message, inner) + { + HelpDocument = helpDocument; + } /// /// Gets the plugin the error is related to /// public Plugin? Plugin { get; } + + /// + /// Gets or sets the help document related to this exception. + /// + public string? HelpDocument { get; } + } \ No newline at end of file diff --git a/src/Artemis.Core/Services/Interfaces/IPluginManagementService.cs b/src/Artemis.Core/Services/Interfaces/IPluginManagementService.cs index 209158529..8e0faf564 100644 --- a/src/Artemis.Core/Services/Interfaces/IPluginManagementService.cs +++ b/src/Artemis.Core/Services/Interfaces/IPluginManagementService.cs @@ -127,6 +127,13 @@ public interface IPluginManagementService : IArtemisService, IDisposable /// If the current call stack contains a plugin, the plugin. Otherwise null Plugin? GetCallingPlugin(); + /// + /// Returns the plugin that threw the provided exception. + /// + /// + /// If the exception was thrown by a plugin, the plugin. Otherwise null + Plugin? GetPluginFromException(Exception exception); + /// /// Gets the plugin that defined the specified device /// diff --git a/src/Artemis.Core/Services/PluginManagementService.cs b/src/Artemis.Core/Services/PluginManagementService.cs index 06b9b6368..c03aec7bb 100644 --- a/src/Artemis.Core/Services/PluginManagementService.cs +++ b/src/Artemis.Core/Services/PluginManagementService.cs @@ -192,7 +192,19 @@ internal class PluginManagementService : IPluginManagementService public Plugin? GetCallingPlugin() { - StackTrace stackTrace = new(); // get call stack + return GetPluginFromStackTrace(new StackTrace()); + } + + public Plugin? GetPluginFromException(Exception exception) + { + if (exception is ArtemisPluginException pluginException && pluginException.Plugin != null) + return pluginException.Plugin; + + return GetPluginFromStackTrace(new StackTrace(exception)); + } + + private Plugin? GetPluginFromStackTrace(StackTrace stackTrace) + { StackFrame[] stackFrames = stackTrace.GetFrames(); // get method calls (frames) foreach (StackFrame stackFrame in stackFrames)