Skip to content

Commit b588b94

Browse files
authored
Merge pull request #271 from taooceros/HttpErrorHandling
Http error handling
2 parents aa0ee75 + bc63b9e commit b588b94

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

Flow.Launcher.Infrastructure/Http/Http.cs

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,31 @@ public static void UpdateProxy(ProxyProperty property)
7575
};
7676
}
7777

78-
public static async Task Download([NotNull] string url, [NotNull] string filePath)
78+
public static async Task DownloadAsync([NotNull] string url, [NotNull] string filePath)
7979
{
80-
using var response = await client.GetAsync(url);
81-
if (response.StatusCode == HttpStatusCode.OK)
80+
try
8281
{
83-
await using var fileStream = new FileStream(filePath, FileMode.CreateNew);
84-
await response.Content.CopyToAsync(fileStream);
82+
using var response = await client.GetAsync(url);
83+
if (response.StatusCode == HttpStatusCode.OK)
84+
{
85+
await using var fileStream = new FileStream(filePath, FileMode.CreateNew);
86+
await response.Content.CopyToAsync(fileStream);
87+
}
88+
else
89+
{
90+
throw new HttpRequestException($"Error code <{response.StatusCode}> returned from <{url}>");
91+
}
8592
}
86-
else
93+
catch (HttpRequestException e)
8794
{
88-
throw new HttpRequestException($"Error code <{response.StatusCode}> returned from <{url}>");
95+
Log.Exception("Infrastructure.Http", "Http Request Error", e, "DownloadAsync");
96+
throw;
8997
}
9098
}
9199

92100
/// <summary>
93101
/// Asynchrously get the result as string from url.
94-
/// When supposing the result is long and large, try using GetStreamAsync to avoid reading as string
102+
/// When supposing the result larger than 83kb, try using GetStreamAsync to avoid reading as string
95103
/// </summary>
96104
/// <param name="url"></param>
97105
/// <returns></returns>
@@ -101,19 +109,33 @@ public static Task<string> GetAsync([NotNull] string url)
101109
return GetAsync(new Uri(url.Replace("#", "%23")));
102110
}
103111

112+
/// <summary>
113+
/// Asynchrously get the result as string from url.
114+
/// When supposing the result larger than 83kb, try using GetStreamAsync to avoid reading as string
115+
/// </summary>
116+
/// <param name="url"></param>
117+
/// <returns></returns>
104118
public static async Task<string> GetAsync([NotNull] Uri url)
105119
{
106120
Log.Debug($"|Http.Get|Url <{url}>");
107-
using var response = await client.GetAsync(url);
108-
var content = await response.Content.ReadAsStringAsync();
109-
if (response.StatusCode == HttpStatusCode.OK)
121+
try
110122
{
111-
return content;
123+
using var response = await client.GetAsync(url);
124+
var content = await response.Content.ReadAsStringAsync();
125+
if (response.StatusCode == HttpStatusCode.OK)
126+
{
127+
return content;
128+
}
129+
else
130+
{
131+
throw new HttpRequestException(
132+
$"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>");
133+
}
112134
}
113-
else
135+
catch (HttpRequestException e)
114136
{
115-
throw new HttpRequestException(
116-
$"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>");
137+
Log.Exception("Infrastructure.Http", "Http Request Error", e, "GetAsync");
138+
throw;
117139
}
118140
}
119141

@@ -124,9 +146,17 @@ public static async Task<string> GetAsync([NotNull] Uri url)
124146
/// <returns></returns>
125147
public static async Task<Stream> GetStreamAsync([NotNull] string url)
126148
{
127-
Log.Debug($"|Http.Get|Url <{url}>");
128-
var response = await client.GetAsync(url);
129-
return await response.Content.ReadAsStreamAsync();
149+
try
150+
{
151+
Log.Debug($"|Http.Get|Url <{url}>");
152+
var response = await client.GetAsync(url);
153+
return await response.Content.ReadAsStreamAsync();
154+
}
155+
catch (HttpRequestException e)
156+
{
157+
Log.Exception("Infrastructure.Http", "Http Request Error", e, "GetStreamAsync");
158+
throw;
159+
}
130160
}
131161
}
132162
}

Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ internal async Task InstallOrUpdate(UserPlugin plugin)
127127
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
128128
Context.API.GetTranslation("plugin_pluginsmanager_please_wait"));
129129

130-
await Http.Download(plugin.UrlDownload, filePath).ConfigureAwait(false);
130+
await Http.DownloadAsync(plugin.UrlDownload, filePath).ConfigureAwait(false);
131131

132132
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
133133
Context.API.GetTranslation("plugin_pluginsmanager_download_success"));
@@ -217,7 +217,7 @@ on existingPlugin.Metadata.ID equals pluginFromManifest.ID
217217
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
218218
Context.API.GetTranslation("plugin_pluginsmanager_please_wait"));
219219

220-
await Http.Download(x.PluginNewUserPlugin.UrlDownload, downloadToFilePath).ConfigureAwait(false);
220+
await Http.DownloadAsync(x.PluginNewUserPlugin.UrlDownload, downloadToFilePath).ConfigureAwait(false);
221221

222222
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
223223
Context.API.GetTranslation("plugin_pluginsmanager_download_success"));

0 commit comments

Comments
 (0)