Skip to content

Commit

Permalink
[CmdNotFound]Log and runtime error handling (#30745)
Browse files Browse the repository at this point in the history
* log and error handling

* addressed feedbacks
  • Loading branch information
davidegiacometti committed Jan 4, 2024
1 parent 1712cd6 commit 5f2d821
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/modules/cmdNotFound/CmdNotFound/CmdNotFound.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\common\ManagedCommon\ManagedCommon.csproj" />
<ProjectReference Include="..\..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;

Expand All @@ -29,6 +30,8 @@ public sealed class WinGetCommandNotFoundFeedbackPredictor : IFeedbackProvider,

private WinGetCommandNotFoundFeedbackPredictor(string guid)
{
Logger.InitializeLogger("\\CmdNotFound\\Logs");

_guid = new Guid(guid);

var provider = new DefaultObjectPoolProvider();
Expand Down Expand Up @@ -69,33 +72,41 @@ private void WarmUp()
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<string>();
foreach (var pkg in pkgList)
{
_candidates.Add(string.Format(CultureInfo.InvariantCulture, "winget install --id {0}", pkg.Members["Id"].Value.ToString()));
}
// Build list of suggestions
_candidates = new List<string>();
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;
// 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());
PowerToysTelemetry.Log.WriteEvent(new Telemetry.CmdNotFoundFeedbackProvidedEvent());

return new FeedbackItem(
"Try installing this package using winget:",
_candidates,
footerMessage,
FeedbackDisplayLayout.Portrait);
return new FeedbackItem(
"Try installing this package using winget:",
_candidates,
footerMessage,
FeedbackDisplayLayout.Portrait);
}
catch (Exception ex)
{
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<string>(), FeedbackDisplayLayout.Portrait);
}
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ class CmdNotFound : public PowertoyModuleIface
{
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();
Expand Down

0 comments on commit 5f2d821

Please sign in to comment.