Skip to content

Commit f60585f

Browse files
committed
Use try-catch to handle download issue
1 parent 8737217 commit f60585f

File tree

1 file changed

+59
-45
lines changed

1 file changed

+59
-45
lines changed

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

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -316,61 +316,75 @@ where string.Compare(existingPlugin.Metadata.Version, pluginUpdateSource.Version
316316
var downloadToFilePath = Path.Combine(Path.GetTempPath(),
317317
$"{x.Name}-{x.NewVersion}.zip");
318318

319-
_ = Task.Run(async delegate
319+
_ = Task.Run(async () =>
320320
{
321-
using var cts = new CancellationTokenSource();
322-
323-
if (!x.PluginNewUserPlugin.IsFromLocalInstallPath)
324-
{
325-
await DownloadFileAsync(
326-
$"{Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin")} {x.PluginNewUserPlugin.Name}",
327-
x.PluginNewUserPlugin.UrlDownload, downloadToFilePath, cts);
328-
}
329-
else
321+
try
330322
{
331-
downloadToFilePath = x.PluginNewUserPlugin.LocalInstallPath;
332-
}
323+
using var cts = new CancellationTokenSource();
333324

334-
// check if user cancelled download before installing plugin
335-
if (cts.IsCancellationRequested)
336-
{
337-
return;
338-
}
339-
else
340-
{
341-
await Context.API.UpdatePluginAsync(x.PluginExistingMetadata, x.PluginNewUserPlugin,
342-
downloadToFilePath);
325+
if (!x.PluginNewUserPlugin.IsFromLocalInstallPath)
326+
{
327+
await DownloadFileAsync(
328+
$"{Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin")} {x.PluginNewUserPlugin.Name}",
329+
x.PluginNewUserPlugin.UrlDownload, downloadToFilePath, cts);
330+
}
331+
else
332+
{
333+
downloadToFilePath = x.PluginNewUserPlugin.LocalInstallPath;
334+
}
343335

344-
if (Settings.AutoRestartAfterChanging)
336+
// check if user cancelled download before installing plugin
337+
if (cts.IsCancellationRequested)
345338
{
346-
Context.API.ShowMsg(
347-
Context.API.GetTranslation("plugin_pluginsmanager_update_title"),
348-
string.Format(
349-
Context.API.GetTranslation(
350-
"plugin_pluginsmanager_update_success_restart"),
351-
x.Name));
352-
Context.API.RestartApp();
339+
return;
353340
}
354341
else
355342
{
356-
Context.API.ShowMsg(
357-
Context.API.GetTranslation("plugin_pluginsmanager_update_title"),
358-
string.Format(
359-
Context.API.GetTranslation(
360-
"plugin_pluginsmanager_update_success_no_restart"),
361-
x.Name));
343+
await Context.API.UpdatePluginAsync(x.PluginExistingMetadata, x.PluginNewUserPlugin,
344+
downloadToFilePath);
345+
346+
if (Settings.AutoRestartAfterChanging)
347+
{
348+
Context.API.ShowMsg(
349+
Context.API.GetTranslation("plugin_pluginsmanager_update_title"),
350+
string.Format(
351+
Context.API.GetTranslation(
352+
"plugin_pluginsmanager_update_success_restart"),
353+
x.Name));
354+
Context.API.RestartApp();
355+
}
356+
else
357+
{
358+
Context.API.ShowMsg(
359+
Context.API.GetTranslation("plugin_pluginsmanager_update_title"),
360+
string.Format(
361+
Context.API.GetTranslation(
362+
"plugin_pluginsmanager_update_success_no_restart"),
363+
x.Name));
364+
}
362365
}
363366
}
364-
}).ContinueWith(t =>
365-
{
366-
Context.API.LogException(ClassName, $"Update failed for {x.Name}",
367-
t.Exception.InnerException);
368-
Context.API.ShowMsg(
369-
Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"),
370-
string.Format(
371-
Context.API.GetTranslation("plugin_pluginsmanager_install_error_subtitle"),
372-
x.Name));
373-
}, token, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default);
367+
catch (HttpRequestException e)
368+
{
369+
// show error message
370+
Context.API.ShowMsgError(
371+
string.Format(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"), x.Name),
372+
Context.API.GetTranslation("plugin_pluginsmanager_download_error"));
373+
Context.API.LogException(ClassName, "An error occurred while downloading plugin", e);
374+
return;
375+
}
376+
catch (Exception e)
377+
{
378+
// show error message
379+
Context.API.LogException(ClassName, $"Update failed for {x.Name}", e);
380+
Context.API.ShowMsgError(
381+
Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"),
382+
string.Format(
383+
Context.API.GetTranslation("plugin_pluginsmanager_install_error_subtitle"),
384+
x.Name));
385+
return;
386+
}
387+
});
374388

375389
return true;
376390
},

0 commit comments

Comments
 (0)