Skip to content

Free, open-source Windows optimization tool for performance, privacy, and simplicity.

fix(optimizationhelper): add try catch

itsfatduckbe3fe0063cc7
diff --git a/optimizerDuck/src/Core/Helpers/OptimizationHelper.cs b/optimizerDuck/src/Core/Helpers/OptimizationHelper.cs
index 80b6103..e4ba79a 100644
--- a/optimizerDuck/src/Core/Helpers/OptimizationHelper.cs
+++ b/optimizerDuck/src/Core/Helpers/OptimizationHelper.cs
@@ -82,7 +82,9 @@ public static class OptimizationHelper
public static AppxClassification GetBloatwareChoices()
{
- var result = ShellService.PowerShell($$"""
+ try
+ {
+ var result = ShellService.PowerShell($$"""
# list the SAFE_APPS
$safeApps = @({{string.Join(", ", Defaults.SAFE_APPS.Keys.Select(x => $"\"{x}\""))}})
@@ -130,89 +132,95 @@ public static class OptimizationHelper
""");
- var raw = JsonConvert.DeserializeObject<AppxClassificationRaw>(result.Stdout);
+ var raw = JsonConvert.DeserializeObject<AppxClassificationRaw>(result.Stdout);
- var allSafe = raw.SafeApps.SelectMany(x => x).ToList();
- var allCaution = raw.CautionApps.SelectMany(x => x).ToList();
+ var allSafe = raw.SafeApps.SelectMany(x => x).ToList();
+ var allCaution = raw.CautionApps.SelectMany(x => x).ToList();
- // Display Length
- var safeDisplayLength = allSafe.Count != 0
- ? allSafe.Max(x => Defaults.SAFE_APPS.TryGetValue(x.Name, out var dn) ? dn.Length : x.Name.Length)
- : 0;
+ // Display Length
+ var safeDisplayLength = allSafe.Count != 0
+ ? allSafe.Max(x => Defaults.SAFE_APPS.TryGetValue(x.Name, out var dn) ? dn.Length : x.Name.Length)
+ : 0;
- var cautionDisplayLength = allCaution.Count != 0
- ? allCaution.Max(x => Defaults.CAUTION_APPS.TryGetValue(x.Name, out var dn) ? dn.Length : x.Name.Length)
- : 0;
+ var cautionDisplayLength = allCaution.Count != 0
+ ? allCaution.Max(x => Defaults.CAUTION_APPS.TryGetValue(x.Name, out var dn) ? dn.Length : x.Name.Length)
+ : 0;
- // Version Length
- var safeVersionLength = allSafe.Count != 0
- ? allSafe.Max(x => x.Version.Length)
- : 0;
+ // Version Length
+ var safeVersionLength = allSafe.Count != 0
+ ? allSafe.Max(x => x.Version.Length)
+ : 0;
- var cautionVersionLength = allCaution.Count != 0
- ? allCaution.Max(x => x.Version.Length)
- : 0;
+ var cautionVersionLength = allCaution.Count != 0
+ ? allCaution.Max(x => x.Version.Length)
+ : 0;
- var maxDisplayNameLength = Math.Max(safeDisplayLength, cautionDisplayLength) + 1;
+ var maxDisplayNameLength = Math.Max(safeDisplayLength, cautionDisplayLength) + 1;
- var maxVersionLength = Math.Max(safeVersionLength, cautionVersionLength) + 1;
+ var maxVersionLength = Math.Max(safeVersionLength, cautionVersionLength) + 1;
- var classification = new AppxClassification(
- allSafe
- .Select(app =>
- {
- return app with
+ var classification = new AppxClassification(
+ allSafe
+ .Select(app =>
+ {
+ return app with
+ {
+ DisplayName = Defaults.SAFE_APPS.TryGetValue(app.Name, out var dn) ? dn : app.Name // get display name or fallback to app name
+ };
+ })
+ .OrderBy(x => x.DisplayName)
+ .Select(app => app with
{
- DisplayName = Defaults.SAFE_APPS.TryGetValue(app.Name, out var dn) ? dn : app.Name // get display name or fallback to app name
- };
- })
- .OrderBy(x => x.DisplayName)
- .Select(app => app with
- {
- DisplayName = app.DisplayName.PadRight(maxDisplayNameLength),
- Version = app.Version.PadRight(maxVersionLength)
- })
- .ToList(),
- allCaution
- .Select(app =>
- {
- return app with
+ DisplayName = app.DisplayName.PadRight(maxDisplayNameLength),
+ Version = app.Version.PadRight(maxVersionLength)
+ })
+ .ToList(),
+ allCaution
+ .Select(app =>
{
- DisplayName = Defaults.CAUTION_APPS.TryGetValue(app.Name, out var dn) ? dn : app.Name
- };
- })
- .OrderBy(x => x.DisplayName)
- .Select(app => app with
- {
- DisplayName = app.DisplayName.PadRight(maxDisplayNameLength),
- Version = app.Version.PadRight(maxVersionLength)
- })
- .ToList()
- );
-
- var parts = new List<string>();
-
- if (classification.SafeApps.Count > 0)
- {
- parts.Add($"[{Theme.Success}]{classification.SafeApps.Count}[/] safe apps");
- Log.LogDebug("Found {TotalSafeApps} safe apps: {SafeApps}", classification.SafeApps.Count,
- string.Join(", ", classification.SafeApps.Select(x => x.Name).ToList()));
- }
+ return app with
+ {
+ DisplayName = Defaults.CAUTION_APPS.TryGetValue(app.Name, out var dn) ? dn : app.Name
+ };
+ })
+ .OrderBy(x => x.DisplayName)
+ .Select(app => app with
+ {
+ DisplayName = app.DisplayName.PadRight(maxDisplayNameLength),
+ Version = app.Version.PadRight(maxVersionLength)
+ })
+ .ToList()
+ );
- if (classification.CautionApps.Count > 0)
- {
- parts.Add($"[{Theme.Error}]{classification.CautionApps.Count}[/] caution apps");
- Log.LogDebug("Found {TotalCautionApps} caution apps: {CautionApps}", classification.CautionApps.Count,
- string.Join(", ", classification.CautionApps.Select(x => x.Name).ToList()));
- }
+ var parts = new List<string>();
+
+ if (classification.SafeApps.Count > 0)
+ {
+ parts.Add($"[{Theme.Success}]{classification.SafeApps.Count}[/] safe apps");
+ Log.LogDebug("Found {TotalSafeApps} safe apps: {SafeApps}", classification.SafeApps.Count,
+ string.Join(", ", classification.SafeApps.Select(x => x.Name).ToList()));
+ }
- if (parts.Count != 0)
- Log.LogInformation($"Found {string.Join(" and ", parts)}.");
- else
- Log.LogWarning("No apps found.");
+ if (classification.CautionApps.Count > 0)
+ {
+ parts.Add($"[{Theme.Error}]{classification.CautionApps.Count}[/] caution apps");
+ Log.LogDebug("Found {TotalCautionApps} caution apps: {CautionApps}", classification.CautionApps.Count,
+ string.Join(", ", classification.CautionApps.Select(x => x.Name).ToList()));
+ }
+
+ if (parts.Count != 0)
+ Log.LogInformation($"Found {string.Join(" and ", parts)}.");
+ else
+ Log.LogWarning("No apps found.");
- return classification;
+ return classification;
+ }
+ catch (Exception ex)
+ {
+ Log.LogError(ex, "Failed to get bloatware choices.");
+ return new AppxClassification(new List<AppxPackage>(), new List<AppxPackage>());
+ }
}
}
\ No newline at end of file