From 00187269dea047cdb1baaf8f29467dfa9ac26d4a Mon Sep 17 00:00:00 2001 From: Divyansh Srivastava Date: Thu, 17 Sep 2020 16:17:02 -0700 Subject: [PATCH] handle error in Packaged program loading (#6674) --- .../Programs/PackageWrapper.cs | 4 ++-- .../Programs/PackagemanagerWrapper.cs | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/PackageWrapper.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/PackageWrapper.cs index 0f45c5abf6..1200fab18a 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/PackageWrapper.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/PackageWrapper.cs @@ -49,9 +49,9 @@ namespace Microsoft.Plugin.Program.Programs { path = package.InstalledLocation.Path; } - catch (Exception e) when (e is ArgumentException || e is FileNotFoundException) + catch (Exception e) when (e is ArgumentException || e is FileNotFoundException || e is DirectoryNotFoundException) { - ProgramLogger.LogException($"PackageWrapper", "GetWrapperFromPackage", "package.InstalledLocation.Path", $"Exception {package.Id.Name}", e); + ProgramLogger.LogException($"PackageWrapper", "GetWrapperFromPackage", "Path could not be determined", $"Exception {package.Id.Name}", e); return new PackageWrapper( package.Id.Name, package.Id.FullName, diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/PackagemanagerWrapper.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/PackagemanagerWrapper.cs index 29c87bf2f6..2d4d6ce9df 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/PackagemanagerWrapper.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/PackagemanagerWrapper.cs @@ -2,9 +2,11 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Collections.Generic; using System.Security.Principal; using Windows.Management.Deployment; +using Wox.Infrastructure.Logger; using Package = Windows.ApplicationModel.Package; namespace Microsoft.Plugin.Program.Programs @@ -18,6 +20,7 @@ namespace Microsoft.Plugin.Program.Programs _packageManager = new PackageManager(); } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to catch all exception to prevent error in a program from affecting loading of program plugin.")] public IEnumerable FindPackagesForCurrentUser() { List packages = new List(); @@ -29,7 +32,14 @@ namespace Microsoft.Plugin.Program.Programs var m = _packageManager.FindPackagesForUser(id); foreach (Package p in m) { - packages.Add(PackageWrapper.GetWrapperFromPackage(p)); + try + { + packages.Add(PackageWrapper.GetWrapperFromPackage(p)); + } + catch (Exception e) + { + Log.Error(nameof(PackageManagerWrapper), e.Message, nameof(FindPackagesForCurrentUser)); + } } }