1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-12 21:38:38 +00:00

Plugins - Added constructors to PluginException to provide a help document

This commit is contained in:
Robert 2023-04-08 13:13:58 +02:00
parent 69c623bc8f
commit 2fcc6d7862
3 changed files with 45 additions and 4 deletions

View File

@ -10,7 +10,7 @@ public class ArtemisPluginException : Exception
/// <summary>
/// Creates a new instance of the <see cref="ArtemisPluginException" /> class
/// </summary>
public ArtemisPluginException(Plugin plugin)
internal ArtemisPluginException(Plugin plugin)
{
Plugin = plugin;
}
@ -18,7 +18,7 @@ public class ArtemisPluginException : Exception
/// <summary>
/// Creates a new instance of the <see cref="ArtemisPluginException" /> class
/// </summary>
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
/// <summary>
/// Creates a new instance of the <see cref="ArtemisPluginException" /> class
/// </summary>
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)
{
}
/// <summary>
/// Creates a new instance of the <see cref="ArtemisPluginException" /> class
/// </summary>
public ArtemisPluginException(string message, string helpDocument) : base(message)
{
HelpDocument = helpDocument;
}
/// <summary>
/// Creates a new instance of the <see cref="ArtemisPluginException" /> class
/// </summary>
public ArtemisPluginException(string message, Exception inner, string helpDocument) : base(message, inner)
{
HelpDocument = helpDocument;
}
/// <summary>
/// Gets the plugin the error is related to
/// </summary>
public Plugin? Plugin { get; }
/// <summary>
/// Gets or sets the help document related to this exception.
/// </summary>
public string? HelpDocument { get; }
}

View File

@ -127,6 +127,13 @@ public interface IPluginManagementService : IArtemisService, IDisposable
/// <returns>If the current call stack contains a plugin, the plugin. Otherwise null</returns>
Plugin? GetCallingPlugin();
/// <summary>
/// Returns the plugin that threw the provided exception.
/// </summary>
/// <param name="exception"></param>
/// <returns>If the exception was thrown by a plugin, the plugin. Otherwise null</returns>
Plugin? GetPluginFromException(Exception exception);
/// <summary>
/// Gets the plugin that defined the specified device
/// </summary>

View File

@ -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)