From 7e9d4b0b0b222612499f36aba63b272148cfe33e Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sat, 23 Apr 2022 12:35:02 -0500 Subject: [PATCH] Plugin Exception Draft --- .../ExternalPlugins/FlowPluginException.cs | 24 ++++++++++++++ Flow.Launcher.Core/Plugin/PluginManager.cs | 16 +++++---- Flow.Launcher/ReportWindow.xaml.cs | 33 ++++++++++++++++--- 3 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 Flow.Launcher.Core/ExternalPlugins/FlowPluginException.cs diff --git a/Flow.Launcher.Core/ExternalPlugins/FlowPluginException.cs b/Flow.Launcher.Core/ExternalPlugins/FlowPluginException.cs new file mode 100644 index 00000000000..47bc285c9f1 --- /dev/null +++ b/Flow.Launcher.Core/ExternalPlugins/FlowPluginException.cs @@ -0,0 +1,24 @@ +using Flow.Launcher.Plugin; +using System; + +namespace Flow.Launcher.Core.ExternalPlugins +{ + public class FlowPluginException : Exception + { + public PluginMetadata Metadata { get; set; } + + public FlowPluginException(PluginMetadata metadata, Exception e) : base(e.Message, e) + { + Metadata = metadata; + } + + public override string ToString() + { + return $@"{Metadata.Name} Exception: +Websites: {Metadata.Website} +Author: {Metadata.Author} +Version: {Metadata.Version} +{base.ToString()}"; + } + } +} \ No newline at end of file diff --git a/Flow.Launcher.Core/Plugin/PluginManager.cs b/Flow.Launcher.Core/Plugin/PluginManager.cs index 134c3c002dc..b9ea650f493 100644 --- a/Flow.Launcher.Core/Plugin/PluginManager.cs +++ b/Flow.Launcher.Core/Plugin/PluginManager.cs @@ -1,4 +1,5 @@ -using System; +using Flow.Launcher.Core.ExternalPlugins; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; @@ -182,13 +183,11 @@ public static ICollection ValidPluginsForQuery(Query query) public static async Task> QueryForPluginAsync(PluginPair pair, Query query, CancellationToken token) { var results = new List(); + var metadata = pair.Metadata; + try { - var metadata = pair.Metadata; - - long milliseconds = -1L; - - milliseconds = await Stopwatch.DebugAsync($"|PluginManager.QueryForPlugin|Cost for {metadata.Name}", + var milliseconds = await Stopwatch.DebugAsync($"|PluginManager.QueryForPlugin|Cost for {metadata.Name}", async () => results = await pair.Plugin.QueryAsync(query, token).ConfigureAwait(false)); token.ThrowIfCancellationRequested(); @@ -206,7 +205,10 @@ public static async Task> QueryForPluginAsync(PluginPair pair, Quer // null will be fine since the results will only be added into queue if the token hasn't been cancelled return null; } - + catch (Exception e) + { + throw new FlowPluginException(metadata, e); + } return results; } diff --git a/Flow.Launcher/ReportWindow.xaml.cs b/Flow.Launcher/ReportWindow.xaml.cs index 6a9fd60e0b0..72c69d74d17 100644 --- a/Flow.Launcher/ReportWindow.xaml.cs +++ b/Flow.Launcher/ReportWindow.xaml.cs @@ -1,4 +1,5 @@ -using System; +using Flow.Launcher.Core.ExternalPlugins; +using System; using System.Diagnostics; using System.Globalization; using System.IO; @@ -22,13 +23,34 @@ public ReportWindow(Exception exception) SetException(exception); } + private static string GetIssueUrl(string website) + { + if (!website.StartsWith("https://github.com")) + { + return website; + } + if(website.Contains("Flow-Launcher/Flow.Launcher")) + { + return Constant.Issue; + } + var treeIndex = website.IndexOf("tree", StringComparison.Ordinal); + return treeIndex == -1 ? $"{website}/issues/new" : $"{website[..treeIndex]}/issues/new"; + } + private void SetException(Exception exception) { string path = Log.CurrentLogDirectory; var directory = new DirectoryInfo(path); var log = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).First(); - var paragraph = Hyperlink("Please open new issue in: ", Constant.Issue); + var websiteUrl = exception switch + { + FlowPluginException pluginException =>GetIssueUrl(pluginException.Metadata.Website), + _ => Constant.Issue + }; + + + var paragraph = Hyperlink("Please open new issue in: ", websiteUrl); paragraph.Inlines.Add($"1. upload log file: {log.FullName}\n"); paragraph.Inlines.Add($"2. copy below exception message"); ErrorTextbox.Document.Blocks.Add(paragraph); @@ -49,7 +71,10 @@ private Paragraph Hyperlink(string textBeforeUrl, string url) var paragraph = new Paragraph(); paragraph.Margin = new Thickness(0); - var link = new Hyperlink { IsEnabled = true }; + var link = new Hyperlink + { + IsEnabled = true + }; link.Inlines.Add(url); link.NavigateUri = new Uri(url); link.RequestNavigate += (s, e) => SearchWeb.OpenInBrowserTab(e.Uri.ToString()); @@ -62,4 +87,4 @@ private Paragraph Hyperlink(string textBeforeUrl, string url) return paragraph; } } -} +} \ No newline at end of file