mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
Add exception handling in win32 program (#6958)
* Add exception handling to prevent program from failing due to error in one program * Error handling for program path function * Fix incorrect log value in ProgramLogger
This commit is contained in:
committed by
GitHub
parent
71765238b1
commit
5d095efe90
@@ -330,6 +330,7 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
return ExecutableName;
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Any error in CreateWin32Program should not prevent other programs from loading.")]
|
||||
private static Win32Program CreateWin32Program(string path)
|
||||
{
|
||||
try
|
||||
@@ -353,12 +354,21 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
{
|
||||
ProgramLogger.Exception($"|Permission denied when trying to load the program from {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);
|
||||
|
||||
return new Win32Program() { Valid = false, Enabled = false };
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ProgramLogger.Exception($"|An unexpected error occurred in the calling method CreateWin32Program at {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);
|
||||
|
||||
return new Win32Program() { Valid = false, Enabled = false };
|
||||
}
|
||||
}
|
||||
|
||||
// This function filters Internet Shortcut programs
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Any error in InternetShortcutProgram should not prevent other programs from loading.")]
|
||||
private static Win32Program InternetShortcutProgram(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
string[] lines = FileWrapper.ReadAllLines(path);
|
||||
string iconPath = string.Empty;
|
||||
@@ -429,13 +439,20 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
return new Win32Program() { Valid = false, Enabled = false };
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ProgramLogger.Exception($"|An unexpected error occurred in the calling method InternetShortcutProgram at {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);
|
||||
|
||||
return new Win32Program() { Valid = false, Enabled = false };
|
||||
}
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Unsure of what exceptions are caught here while enabling static analysis")]
|
||||
private static Win32Program LnkProgram(string path)
|
||||
{
|
||||
var program = CreateWin32Program(path);
|
||||
try
|
||||
{
|
||||
var program = CreateWin32Program(path);
|
||||
const int MAX_PATH = 260;
|
||||
StringBuilder buffer = new StringBuilder(MAX_PATH);
|
||||
|
||||
@@ -472,13 +489,13 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
// Error caused likely due to trying to get the description of the program
|
||||
catch (Exception e)
|
||||
{
|
||||
ProgramLogger.Exception("An unexpected error occurred in the calling method LnkProgram", e, MethodBase.GetCurrentMethod().DeclaringType, path);
|
||||
ProgramLogger.Exception($"|An unexpected error occurred in the calling method LnkProgram at {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);
|
||||
|
||||
program.Valid = false;
|
||||
return program;
|
||||
return new Win32Program() { Valid = false, Enabled = false };
|
||||
}
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Any error in ExeProgram should not prevent other programs from loading.")]
|
||||
private static Win32Program ExeProgram(string path)
|
||||
{
|
||||
try
|
||||
@@ -503,6 +520,12 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
{
|
||||
ProgramLogger.Exception($"|Unable to locate exe file at {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);
|
||||
|
||||
return new Win32Program() { Valid = false, Enabled = false };
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ProgramLogger.Exception($"|An unexpected error occurred in the calling method ExeProgram at {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);
|
||||
|
||||
return new Win32Program() { Valid = false, Enabled = false };
|
||||
}
|
||||
}
|
||||
@@ -585,6 +608,7 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
}
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Minimise the effect of error on other programs")]
|
||||
private static IEnumerable<string> ProgramPaths(string directory, IList<string> suffixes, bool recursiveSearch = true)
|
||||
{
|
||||
if (!Directory.Exists(directory))
|
||||
@@ -617,6 +641,10 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
{
|
||||
ProgramLogger.Exception($"|Permission denied when trying to load programs from {currentDirectory}", e, MethodBase.GetCurrentMethod().DeclaringType, currentDirectory);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ProgramLogger.Exception($"|An unexpected error occurred in the calling method ProgramPaths at {currentDirectory}", e, MethodBase.GetCurrentMethod().DeclaringType, currentDirectory);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
@@ -635,6 +663,10 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
{
|
||||
ProgramLogger.Exception($"|Permission denied when trying to load programs from {currentDirectory}", e, MethodBase.GetCurrentMethod().DeclaringType, currentDirectory);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ProgramLogger.Exception($"|An unexpected error occurred in the calling method ProgramPaths at {currentDirectory}", e, MethodBase.GetCurrentMethod().DeclaringType, currentDirectory);
|
||||
}
|
||||
}
|
||||
while (folderQueue.Any());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user