Skip to content

Commit fa7057a

Browse files
authored
Plugin Exception Draft (#1147)
1 parent af7efd8 commit fa7057a

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Flow.Launcher.Plugin;
2+
using System;
3+
4+
namespace Flow.Launcher.Core.ExternalPlugins
5+
{
6+
public class FlowPluginException : Exception
7+
{
8+
public PluginMetadata Metadata { get; set; }
9+
10+
public FlowPluginException(PluginMetadata metadata, Exception e) : base(e.Message, e)
11+
{
12+
Metadata = metadata;
13+
}
14+
15+
public override string ToString()
16+
{
17+
return $@"{Metadata.Name} Exception:
18+
Websites: {Metadata.Website}
19+
Author: {Metadata.Author}
20+
Version: {Metadata.Version}
21+
{base.ToString()}";
22+
}
23+
}
24+
}

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Flow.Launcher.Core.ExternalPlugins;
2+
using System;
23
using System.Collections.Concurrent;
34
using System.Collections.Generic;
45
using System.IO;
@@ -182,13 +183,11 @@ public static ICollection<PluginPair> ValidPluginsForQuery(Query query)
182183
public static async Task<List<Result>> QueryForPluginAsync(PluginPair pair, Query query, CancellationToken token)
183184
{
184185
var results = new List<Result>();
186+
var metadata = pair.Metadata;
187+
185188
try
186189
{
187-
var metadata = pair.Metadata;
188-
189-
long milliseconds = -1L;
190-
191-
milliseconds = await Stopwatch.DebugAsync($"|PluginManager.QueryForPlugin|Cost for {metadata.Name}",
190+
var milliseconds = await Stopwatch.DebugAsync($"|PluginManager.QueryForPlugin|Cost for {metadata.Name}",
192191
async () => results = await pair.Plugin.QueryAsync(query, token).ConfigureAwait(false));
193192

194193
token.ThrowIfCancellationRequested();
@@ -206,7 +205,10 @@ public static async Task<List<Result>> QueryForPluginAsync(PluginPair pair, Quer
206205
// null will be fine since the results will only be added into queue if the token hasn't been cancelled
207206
return null;
208207
}
209-
208+
catch (Exception e)
209+
{
210+
throw new FlowPluginException(metadata, e);
211+
}
210212
return results;
211213
}
212214

Flow.Launcher/ReportWindow.xaml.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Flow.Launcher.Core.ExternalPlugins;
2+
using System;
23
using System.Diagnostics;
34
using System.Globalization;
45
using System.IO;
@@ -22,13 +23,34 @@ public ReportWindow(Exception exception)
2223
SetException(exception);
2324
}
2425

26+
private static string GetIssueUrl(string website)
27+
{
28+
if (!website.StartsWith("https://github.com"))
29+
{
30+
return website;
31+
}
32+
if(website.Contains("Flow-Launcher/Flow.Launcher"))
33+
{
34+
return Constant.Issue;
35+
}
36+
var treeIndex = website.IndexOf("tree", StringComparison.Ordinal);
37+
return treeIndex == -1 ? $"{website}/issues/new" : $"{website[..treeIndex]}/issues/new";
38+
}
39+
2540
private void SetException(Exception exception)
2641
{
2742
string path = Log.CurrentLogDirectory;
2843
var directory = new DirectoryInfo(path);
2944
var log = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).First();
3045

31-
var paragraph = Hyperlink("Please open new issue in: ", Constant.Issue);
46+
var websiteUrl = exception switch
47+
{
48+
FlowPluginException pluginException =>GetIssueUrl(pluginException.Metadata.Website),
49+
_ => Constant.Issue
50+
};
51+
52+
53+
var paragraph = Hyperlink("Please open new issue in: ", websiteUrl);
3254
paragraph.Inlines.Add($"1. upload log file: {log.FullName}\n");
3355
paragraph.Inlines.Add($"2. copy below exception message");
3456
ErrorTextbox.Document.Blocks.Add(paragraph);
@@ -49,7 +71,10 @@ private Paragraph Hyperlink(string textBeforeUrl, string url)
4971
var paragraph = new Paragraph();
5072
paragraph.Margin = new Thickness(0);
5173

52-
var link = new Hyperlink { IsEnabled = true };
74+
var link = new Hyperlink
75+
{
76+
IsEnabled = true
77+
};
5378
link.Inlines.Add(url);
5479
link.NavigateUri = new Uri(url);
5580
link.RequestNavigate += (s, e) => SearchWeb.OpenInBrowserTab(e.Uri.ToString());
@@ -62,4 +87,4 @@ private Paragraph Hyperlink(string textBeforeUrl, string url)
6287
return paragraph;
6388
}
6489
}
65-
}
90+
}

0 commit comments

Comments
 (0)