From 7739ac73fdd33f16a92f28f5251daace0640e056 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Wed, 24 Feb 2021 15:55:03 +0800 Subject: [PATCH] change exception handling in Updater and WebSearch suggestion source --- Flow.Launcher.Core/Updater.cs | 2 +- .../SuggestionSources/Baidu.cs | 13 +++++++------ .../SuggestionSources/Bing.cs | 10 +++++----- .../SuggestionSources/Google.cs | 10 +++++----- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Flow.Launcher.Core/Updater.cs b/Flow.Launcher.Core/Updater.cs index 9f9c0c2d1..76713ce2a 100644 --- a/Flow.Launcher.Core/Updater.cs +++ b/Flow.Launcher.Core/Updater.cs @@ -87,7 +87,7 @@ namespace Flow.Launcher.Core UpdateManager.RestartApp(Constant.ApplicationFileName); } } - catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException || e is OperationCanceledException) + catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException || e.InnerException is TimeoutException) { Log.Exception($"|Updater.UpdateApp|Check your connection and proxy settings to github-cloud.s3.amazonaws.com.", e); api.ShowMsg(api.GetTranslation("update_flowlauncher_fail"), diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs index 38a589797..94458ff52 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs @@ -25,15 +25,16 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources const string api = "http://suggestion.baidu.com/su?json=1&wd="; result = await Http.GetAsync(api + Uri.EscapeUriString(query), token).ConfigureAwait(false); } - catch (OperationCanceledException) - { - return null; - } - catch (HttpRequestException e) + catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException) { Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e); return null; } + catch (OperationCanceledException) + { + return null; + } + if (string.IsNullOrEmpty(result)) return new List(); Match match = _reg.Match(result); @@ -44,7 +45,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources { json = JsonDocument.Parse(match.Groups[1].Value); } - catch(JsonException e) + catch (JsonException e) { Log.Exception("|Baidu.Suggestions|can't parse suggestions", e); return new List(); diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs index 10de86820..28868100f 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs @@ -28,15 +28,15 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources json = (await JsonDocument.ParseAsync(resultStream, cancellationToken: token)).RootElement.GetProperty("AS"); } + catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException) + { + Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e); + return null; + } catch (OperationCanceledException) { return new List(); } - catch (HttpRequestException e) - { - Log.Exception("|Bing.Suggestions|Can't get suggestion from Bing", e); - return new List(); - } catch (JsonException e) { Log.Exception("|Bing.Suggestions|can't parse suggestions", e); diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs index 5f1ecb31b..a06deea29 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs @@ -27,15 +27,15 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources json = await JsonDocument.ParseAsync(resultStream, cancellationToken: token); } + catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException) + { + Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e); + return null; + } catch (OperationCanceledException) { return new List(); } - catch (HttpRequestException e) - { - Log.Exception("|Google.Suggestions|Can't get suggestion from google", e); - return new List(); - } catch (JsonException e) { Log.Exception("|Google.Suggestions|can't parse suggestions", e);