From 974efc55ac7b9a6f84d58996de3b4bc4822af17b Mon Sep 17 00:00:00 2001 From: bao-qian Date: Mon, 30 Jan 2017 00:26:11 +0000 Subject: [PATCH] remove custom exception --- Wox.Core/Plugin/JsonRPCPlugin.cs | 48 ++++++++++++------- Wox.Core/Plugin/PluginManager.cs | 5 +- Wox.Core/Resource/Internationalization.cs | 40 ++++++++++------ Wox.Core/Resource/ResourceMerger.cs | 3 +- Wox.Infrastructure/Exception/WoxException.cs | 19 -------- .../Exception/WoxFatalException.cs | 12 ----- .../Exception/WoxHttpException.cs | 9 ---- .../Exception/WoxI18nException.cs | 9 ---- .../Exception/WoxJsonRPCException.cs | 10 ---- .../Exception/WoxPluginException.cs | 18 ------- Wox.Infrastructure/Wox.Infrastructure.csproj | 6 --- Wox/Helper/ErrorReporting.cs | 4 -- 12 files changed, 62 insertions(+), 121 deletions(-) delete mode 100644 Wox.Infrastructure/Exception/WoxException.cs delete mode 100644 Wox.Infrastructure/Exception/WoxFatalException.cs delete mode 100644 Wox.Infrastructure/Exception/WoxHttpException.cs delete mode 100644 Wox.Infrastructure/Exception/WoxI18nException.cs delete mode 100644 Wox.Infrastructure/Exception/WoxJsonRPCException.cs delete mode 100644 Wox.Infrastructure/Exception/WoxPluginException.cs diff --git a/Wox.Core/Plugin/JsonRPCPlugin.cs b/Wox.Core/Plugin/JsonRPCPlugin.cs index f781d3329f..2855fa1d1c 100644 --- a/Wox.Core/Plugin/JsonRPCPlugin.cs +++ b/Wox.Core/Plugin/JsonRPCPlugin.cs @@ -75,7 +75,7 @@ namespace Wox.Core.Plugin } catch (Exception e) { - Log.Exception($"|Wox.Core.Plugin.JsonRPCPlugin.Query|Exception when query <{query}>", e); + Log.Exception($"|JsonRPCPlugin.Query|Exception when query <{query}>", e); } } return null; @@ -123,39 +123,53 @@ namespace Wox.Core.Plugin { try { - using (Process process = Process.Start(startInfo)) + using (var process = Process.Start(startInfo)) { if (process != null) { - using (StreamReader reader = process.StandardOutput) + using (var standardOutput = process.StandardOutput) { - string result = reader.ReadToEnd(); - if (result.StartsWith("DEBUG:")) + var result = standardOutput.ReadToEnd(); + if (string.IsNullOrEmpty(result)) { - MessageBox.Show(new Form { TopMost = true }, result.Substring(6)); - return ""; - } - if (String.IsNullOrEmpty(result)) - { - using (StreamReader errorReader = process.StandardError) + using (var standardError = process.StandardError) { - string error = errorReader.ReadToEnd(); - if (!String.IsNullOrEmpty(error)) + var error = standardError.ReadToEnd(); + if (!string.IsNullOrEmpty(error)) { - throw new WoxJsonRPCException(error); + Log.Error($"|JsonRPCPlugin.Execute|{error}"); + return string.Empty; + } + else + { + Log.Error("|JsonRPCPlugin.Execute|Empty standard output and standard error."); + return string.Empty; } } } - return result; + else if (result.StartsWith("DEBUG:")) + { + MessageBox.Show(new Form {TopMost = true}, result.Substring(6)); + return string.Empty; + } + else + { + return result; + } } } + else + { + Log.Error("|JsonRPCPlugin.Execute|Can't start new process"); + return string.Empty; + } } } catch (Exception e) { - throw new WoxJsonRPCException(e.Message); + Log.Exception($"|JsonRPCPlugin.Execute|Exception for filename <{startInfo.FileName}> with argument <{startInfo.Arguments}>", e); + return string.Empty; } - return null; } public void Init(PluginInitContext ctx) diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index 0e3a0a4774..472019b935 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -184,12 +184,13 @@ namespace Wox.Core.Plugin }); metadata.QueryCount += 1; metadata.AvgQueryTime = metadata.QueryCount == 1 ? milliseconds : (metadata.AvgQueryTime + milliseconds) / 2; + return results; } catch (Exception e) { - throw new WoxPluginException(pair.Metadata.Name, "QueryForPlugin failed", e); + Log.Exception($"|PluginManager.QueryForPlugin|Exception for plugin <{pair.Metadata.Name}> when query <{query}>", e); + return new List(); } - return results; } public static void UpdatePluginMetadata(List results, PluginMetadata metadata, Query query) diff --git a/Wox.Core/Resource/Internationalization.cs b/Wox.Core/Resource/Internationalization.cs index b6e237ec26..03fbbb6d46 100644 --- a/Wox.Core/Resource/Internationalization.cs +++ b/Wox.Core/Resource/Internationalization.cs @@ -43,31 +43,43 @@ namespace Wox.Core.Resource private Language GetLanguageByLanguageCode(string languageCode) { - Language language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.ToLower() == languageCode.ToLower()); + var lowercase = languageCode.ToLower(); + var language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.ToLower() == lowercase); if (language == null) { - throw new WoxI18nException("Invalid language code:" + languageCode); + Log.Error($"|Internationalization.GetLanguageByLanguageCode|Language code can't be found <{languageCode}>"); + return AvailableLanguages.English; + } + else + { + return language; } - - return language; } public void ChangeLanguage(Language language) { - if (language == null) throw new WoxI18nException("language can't be null"); - - string path = GetLanguagePath(language); - if (string.IsNullOrEmpty(path)) + if (language != null) { - path = GetLanguagePath(AvailableLanguages.English); - if (string.IsNullOrEmpty(path)) + string path = GetLanguagePath(language); + if (!string.IsNullOrEmpty(path)) { - throw new Exception("Change Language failed"); + Settings.Language = language.LanguageCode; + ResourceMerger.UpdateResource(this); + } + else + { + Log.Error($"|Internationalization.ChangeLanguage|Language path can't be found <{path}>"); + path = GetLanguagePath(AvailableLanguages.English); + if (string.IsNullOrEmpty(path)) + { + Log.Error($"|Internationalization.ChangeLanguage|Default english language path can't be found <{path}>"); + } } } - - Settings.Language = language.LanguageCode; - ResourceMerger.UpdateResource(this); + else + { + Log.Error("|Internationalization.ChangeLanguage|Language can't be null"); + } } diff --git a/Wox.Core/Resource/ResourceMerger.cs b/Wox.Core/Resource/ResourceMerger.cs index 2d14aaab68..fe677b01ab 100644 --- a/Wox.Core/Resource/ResourceMerger.cs +++ b/Wox.Core/Resource/ResourceMerger.cs @@ -6,6 +6,7 @@ using System.Windows; using Wox.Core.Plugin; using Wox.Plugin; using Wox.Infrastructure.Exception; +using Wox.Infrastructure.Logger; namespace Wox.Core.Resource { @@ -54,7 +55,7 @@ namespace Wox.Core.Resource } else { - throw new WoxPluginException(plugin.Metadata.Name, "Can't find plugin location."); + Log.Error($"|ResourceMerger.UpdatePluginLanguages|Can't find plugin path <{location}> for <{plugin.Metadata.Name}>"); } } diff --git a/Wox.Infrastructure/Exception/WoxException.cs b/Wox.Infrastructure/Exception/WoxException.cs deleted file mode 100644 index 89a61119af..0000000000 --- a/Wox.Infrastructure/Exception/WoxException.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace Wox.Infrastructure.Exception -{ - /// - /// Base Wox Exceptions - /// - public class WoxException : System.Exception - { - public WoxException(string msg) - : base(msg) - { - - } - - public WoxException(string msg, System.Exception innerException) - : base(msg, innerException) - { - } - } -} diff --git a/Wox.Infrastructure/Exception/WoxFatalException.cs b/Wox.Infrastructure/Exception/WoxFatalException.cs deleted file mode 100644 index 6a1da4a62b..0000000000 --- a/Wox.Infrastructure/Exception/WoxFatalException.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace Wox.Infrastructure.Exception -{ - /// - /// Represent exceptions that wox can't handle and MUST close running Wox. - /// - public class WoxFatalException : WoxException - { - public WoxFatalException(string msg) : base(msg) - { - } - } -} diff --git a/Wox.Infrastructure/Exception/WoxHttpException.cs b/Wox.Infrastructure/Exception/WoxHttpException.cs deleted file mode 100644 index 2626a3dc76..0000000000 --- a/Wox.Infrastructure/Exception/WoxHttpException.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Wox.Infrastructure.Exception -{ - public class WoxHttpException :WoxException - { - public WoxHttpException(string msg) : base(msg) - { - } - } -} diff --git a/Wox.Infrastructure/Exception/WoxI18nException.cs b/Wox.Infrastructure/Exception/WoxI18nException.cs deleted file mode 100644 index c06ad78022..0000000000 --- a/Wox.Infrastructure/Exception/WoxI18nException.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Wox.Infrastructure.Exception -{ - public class WoxI18nException : WoxException - { - public WoxI18nException(string msg) : base(msg) - { - } - } -} diff --git a/Wox.Infrastructure/Exception/WoxJsonRPCException.cs b/Wox.Infrastructure/Exception/WoxJsonRPCException.cs deleted file mode 100644 index 88bb3f5ed5..0000000000 --- a/Wox.Infrastructure/Exception/WoxJsonRPCException.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Wox.Infrastructure.Exception -{ - public class WoxJsonRPCException : WoxException - { - public WoxJsonRPCException(string msg) - : base(msg) - { - } - } -} diff --git a/Wox.Infrastructure/Exception/WoxPluginException.cs b/Wox.Infrastructure/Exception/WoxPluginException.cs deleted file mode 100644 index b68af28ab0..0000000000 --- a/Wox.Infrastructure/Exception/WoxPluginException.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace Wox.Infrastructure.Exception -{ - public class WoxPluginException : WoxException - { - public string PluginName { get; set; } - - public WoxPluginException(string pluginName, string msg, System.Exception e) - : base($"{pluginName} : {msg}", e) - { - PluginName = pluginName; - } - - public WoxPluginException(string pluginName, string msg) : base(msg) - { - PluginName = pluginName; - } - } -} diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index 31c4c21287..1251fda12c 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -67,12 +67,6 @@ Properties\SolutionAssemblyInfo.cs - - - - - - diff --git a/Wox/Helper/ErrorReporting.cs b/Wox/Helper/ErrorReporting.cs index 84aff62eb6..f536838fbc 100644 --- a/Wox/Helper/ErrorReporting.cs +++ b/Wox/Helper/ErrorReporting.cs @@ -21,10 +21,6 @@ namespace Wox.Helper Application.Current.MainWindow.Dispatcher.Invoke(() => { Report((Exception)e.ExceptionObject); - if (!(e.ExceptionObject is WoxException)) - { - Environment.Exit(0); - } }); }