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:
Divyansh Srivastava
2020-10-05 09:50:07 -07:00
committed by GitHub
parent 71765238b1
commit 5d095efe90

View File

@@ -330,6 +330,7 @@ namespace Microsoft.Plugin.Program.Programs
return ExecutableName; 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) private static Win32Program CreateWin32Program(string path)
{ {
try 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); 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 }; return new Win32Program() { Valid = false, Enabled = false };
} }
} }
// This function filters Internet Shortcut programs // 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) private static Win32Program InternetShortcutProgram(string path)
{
try
{ {
string[] lines = FileWrapper.ReadAllLines(path); string[] lines = FileWrapper.ReadAllLines(path);
string iconPath = string.Empty; string iconPath = string.Empty;
@@ -429,13 +439,20 @@ namespace Microsoft.Plugin.Program.Programs
return new Win32Program() { Valid = false, Enabled = false }; 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")] [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) private static Win32Program LnkProgram(string path)
{ {
var program = CreateWin32Program(path);
try try
{ {
var program = CreateWin32Program(path);
const int MAX_PATH = 260; const int MAX_PATH = 260;
StringBuilder buffer = new StringBuilder(MAX_PATH); 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 // Error caused likely due to trying to get the description of the program
catch (Exception e) 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 new Win32Program() { Valid = false, Enabled = false };
return program;
} }
} }
[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) private static Win32Program ExeProgram(string path)
{ {
try try
@@ -503,6 +520,12 @@ namespace Microsoft.Plugin.Program.Programs
{ {
ProgramLogger.Exception($"|Unable to locate exe file at {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path); 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 }; 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) private static IEnumerable<string> ProgramPaths(string directory, IList<string> suffixes, bool recursiveSearch = true)
{ {
if (!Directory.Exists(directory)) 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); 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 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); 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()); while (folderQueue.Any());