Skip to content

Commit 230df80

Browse files
committed
Improve CommunityPluginSource http exception handler
1 parent 0d9ec48 commit 230df80

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

Flow.Launcher.Core/ExternalPlugins/CommunityPluginSource.cs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Net;
77
using System.Net.Http;
88
using System.Net.Http.Json;
9+
using System.Net.Sockets;
910
using System.Text.Json;
1011
using System.Text.Json.Serialization;
1112
using System.Threading;
@@ -15,6 +16,8 @@ namespace Flow.Launcher.Core.ExternalPlugins
1516
{
1617
public record CommunityPluginSource(string ManifestFileUrl)
1718
{
19+
private static readonly string ClassName = nameof(CommunityPluginSource);
20+
1821
private string latestEtag = "";
1922

2023
private List<UserPlugin> plugins = new();
@@ -34,36 +37,51 @@ public record CommunityPluginSource(string ManifestFileUrl)
3437
/// </remarks>
3538
public async Task<List<UserPlugin>> FetchAsync(CancellationToken token)
3639
{
37-
Log.Info(nameof(CommunityPluginSource), $"Loading plugins from {ManifestFileUrl}");
40+
Log.Info(ClassName, $"Loading plugins from {ManifestFileUrl}");
3841

3942
var request = new HttpRequestMessage(HttpMethod.Get, ManifestFileUrl);
4043

4144
request.Headers.Add("If-None-Match", latestEtag);
4245

43-
using var response = await Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token)
46+
try
47+
{
48+
using var response = await Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token)
4449
.ConfigureAwait(false);
4550

46-
if (response.StatusCode == HttpStatusCode.OK)
47-
{
48-
plugins = await response.Content
49-
.ReadFromJsonAsync<List<UserPlugin>>(PluginStoreItemSerializationOption, cancellationToken: token)
50-
.ConfigureAwait(false);
51-
latestEtag = response.Headers.ETag?.Tag;
51+
if (response.StatusCode == HttpStatusCode.OK)
52+
{
53+
plugins = await response.Content
54+
.ReadFromJsonAsync<List<UserPlugin>>(PluginStoreItemSerializationOption, cancellationToken: token)
55+
.ConfigureAwait(false);
56+
latestEtag = response.Headers.ETag?.Tag;
5257

53-
Log.Info(nameof(CommunityPluginSource), $"Loaded {plugins.Count} plugins from {ManifestFileUrl}");
54-
return plugins;
58+
Log.Info(ClassName, $"Loaded {plugins.Count} plugins from {ManifestFileUrl}");
59+
return plugins;
60+
}
61+
else if (response.StatusCode == HttpStatusCode.NotModified)
62+
{
63+
Log.Info(ClassName, $"Resource {ManifestFileUrl} has not been modified.");
64+
return plugins;
65+
}
66+
else
67+
{
68+
Log.Warn(ClassName,
69+
$"Failed to load resource {ManifestFileUrl} with response {response.StatusCode}");
70+
return plugins;
71+
}
5572
}
56-
else if (response.StatusCode == HttpStatusCode.NotModified)
73+
catch (Exception e)
5774
{
58-
Log.Info(nameof(CommunityPluginSource), $"Resource {ManifestFileUrl} has not been modified.");
75+
if (e is HttpRequestException or WebException or SocketException || e.InnerException is TimeoutException)
76+
{
77+
Log.Exception(ClassName, $"Check your connection and proxy settings to {ManifestFileUrl}.", e);
78+
}
79+
else
80+
{
81+
Log.Exception(ClassName, "Error Occurred", e);
82+
}
5983
return plugins;
6084
}
61-
else
62-
{
63-
Log.Warn(nameof(CommunityPluginSource),
64-
$"Failed to load resource {ManifestFileUrl} with response {response.StatusCode}");
65-
throw new Exception($"Failed to load resource {ManifestFileUrl} with response {response.StatusCode}");
66-
}
6785
}
6886
}
6987
}

Flow.Launcher.Core/Updater.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ public async Task UpdateAppAsync(bool silentUpdate = true)
9393
}
9494
catch (Exception e)
9595
{
96-
if ((e is HttpRequestException or WebException or SocketException || e.InnerException is TimeoutException))
96+
if (e is HttpRequestException or WebException or SocketException || e.InnerException is TimeoutException)
97+
{
9798
Log.Exception($"|Updater.UpdateApp|Check your connection and proxy settings to github-cloud.s3.amazonaws.com.", e);
99+
}
98100
else
101+
{
99102
Log.Exception($"|Updater.UpdateApp|Error Occurred", e);
103+
}
100104

101105
if (!silentUpdate)
102106
_api.ShowMsg(_api.GetTranslation("update_flowlauncher_fail"),

0 commit comments

Comments
 (0)