From 5f2d8216ad31e07a8b5a860a33d0422a73e0869a Mon Sep 17 00:00:00 2001 From: Davide Giacometti Date: Thu, 4 Jan 2024 19:53:40 +0100 Subject: [PATCH] [CmdNotFound]Log and runtime error handling (#30745) * log and error handling * addressed feedbacks --- .../CmdNotFound/CmdNotFound.csproj | 1 + .../WinGetCommandNotFoundFeedbackPredictor.cs | 57 +++++++++++-------- .../CmdNotFoundModuleInterface/dllmain.cpp | 5 +- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/modules/cmdNotFound/CmdNotFound/CmdNotFound.csproj b/src/modules/cmdNotFound/CmdNotFound/CmdNotFound.csproj index 0d4f12d5b4..6e30973af6 100644 --- a/src/modules/cmdNotFound/CmdNotFound/CmdNotFound.csproj +++ b/src/modules/cmdNotFound/CmdNotFound/CmdNotFound.csproj @@ -61,6 +61,7 @@ + diff --git a/src/modules/cmdNotFound/CmdNotFound/WinGetCommandNotFoundFeedbackPredictor.cs b/src/modules/cmdNotFound/CmdNotFound/WinGetCommandNotFoundFeedbackPredictor.cs index 681b073bad..405898ef35 100644 --- a/src/modules/cmdNotFound/CmdNotFound/WinGetCommandNotFoundFeedbackPredictor.cs +++ b/src/modules/cmdNotFound/CmdNotFound/WinGetCommandNotFoundFeedbackPredictor.cs @@ -8,6 +8,7 @@ using System.Globalization; using System.Management.Automation; using System.Management.Automation.Subsystem.Feedback; using System.Management.Automation.Subsystem.Prediction; +using ManagedCommon; using Microsoft.Extensions.ObjectPool; using Microsoft.PowerToys.Telemetry; @@ -29,6 +30,8 @@ namespace WinGetCommandNotFound private WinGetCommandNotFoundFeedbackPredictor(string guid) { + Logger.InitializeLogger("\\CmdNotFound\\Logs"); + _guid = new Guid(guid); var provider = new DefaultObjectPoolProvider(); @@ -69,33 +72,41 @@ namespace WinGetCommandNotFound var target = (string)context.LastError!.TargetObject; if (target is not null) { - bool tooManySuggestions = false; - string packageMatchFilterField = "command"; - var pkgList = FindPackages(target, ref tooManySuggestions, ref packageMatchFilterField); - if (pkgList.Count == 0) + try { - return null; - } + bool tooManySuggestions = false; + string packageMatchFilterField = "command"; + var pkgList = FindPackages(target, ref tooManySuggestions, ref packageMatchFilterField); + if (pkgList.Count == 0) + { + return null; + } - // Build list of suggestions - _candidates = new List(); - foreach (var pkg in pkgList) + // Build list of suggestions + _candidates = new List(); + foreach (var pkg in pkgList) + { + _candidates.Add(string.Format(CultureInfo.InvariantCulture, "winget install --id {0}", pkg.Members["Id"].Value.ToString())); + } + + // Build footer message + var footerMessage = tooManySuggestions ? + string.Format(CultureInfo.InvariantCulture, "Additional results can be found using \"winget search --{0} {1}\"", packageMatchFilterField, target) : + null; + + PowerToysTelemetry.Log.WriteEvent(new Telemetry.CmdNotFoundFeedbackProvidedEvent()); + + return new FeedbackItem( + "Try installing this package using winget:", + _candidates, + footerMessage, + FeedbackDisplayLayout.Portrait); + } + catch (Exception ex) { - _candidates.Add(string.Format(CultureInfo.InvariantCulture, "winget install --id {0}", pkg.Members["Id"].Value.ToString())); + Logger.LogError("GetFeedback failed to execute", ex); + return new FeedbackItem($"Failed to execute PowerToys Command Not Found.{Environment.NewLine}This is a known issue if PowerShell 7 is installed from the Store or MSIX. If that isn't your case, please report an issue.", new List(), FeedbackDisplayLayout.Portrait); } - - // Build footer message - var footerMessage = tooManySuggestions ? - string.Format(CultureInfo.InvariantCulture, "Additional results can be found using \"winget search --{0} {1}\"", packageMatchFilterField, target) : - null; - - PowerToysTelemetry.Log.WriteEvent(new Telemetry.CmdNotFoundFeedbackProvidedEvent()); - - return new FeedbackItem( - "Try installing this package using winget:", - _candidates, - footerMessage, - FeedbackDisplayLayout.Portrait); } return null; diff --git a/src/modules/cmdNotFound/CmdNotFoundModuleInterface/dllmain.cpp b/src/modules/cmdNotFound/CmdNotFoundModuleInterface/dllmain.cpp index fed395d45d..edb8e6455b 100644 --- a/src/modules/cmdNotFound/CmdNotFoundModuleInterface/dllmain.cpp +++ b/src/modules/cmdNotFound/CmdNotFoundModuleInterface/dllmain.cpp @@ -93,10 +93,7 @@ public: { app_name = GET_RESOURCE_STRING(IDS_CMD_NOT_FOUND_NAME); app_key = ModuleKey; - - std::filesystem::path logFilePath(PTSettingsHelper::get_module_save_folder_location(this->app_key)); - logFilePath.append(LogSettings::cmdNotFoundLogPath); - Logger::init(LogSettings::cmdNotFoundLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location()); + LoggerHelpers::init_logger(app_key, L"ModuleInterface", LogSettings::cmdNotFoundLoggerName); Logger::info("CmdNotFound object is constructing"); powertoys_gpo::gpo_rule_configured_t gpo_rule_configured_value = gpo_policy_enabled_configuration();