[MonacoPreview]Fix crash with FileNotFoundException (#31853)

This commit is contained in:
Jaime Bernardo
2024-03-11 17:24:42 +00:00
committed by GitHub
parent ac9159b9f1
commit 8189c52e39

View File

@@ -109,18 +109,18 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
_webView = new WebView2(); _webView = new WebView2();
_webView.DefaultBackgroundColor = Color.Transparent; _webView.DefaultBackgroundColor = Color.Transparent;
// Checks if dataSource is a string try
if (!(dataSource is string filePath))
{ {
throw new ArgumentException($"{nameof(dataSource)} for {nameof(MonacoPreviewHandlerControl)} must be a string but was a '{typeof(T)}'"); // Checks if dataSource is a string
} if (!(dataSource is string filePath))
{
throw new ArgumentException($"{nameof(dataSource)} for {nameof(MonacoPreviewHandlerControl)} must be a string but was a '{typeof(T)}'");
}
// Check if the file is too big. // Check if the file is too big.
long fileSize = new FileInfo(filePath).Length; long fileSize = new FileInfo(filePath).Length;
if (fileSize < _settings.MaxFileSize) if (fileSize < _settings.MaxFileSize)
{
try
{ {
InitializeIndexFileAndSelectedFile(filePath); InitializeIndexFileAndSelectedFile(filePath);
@@ -197,28 +197,28 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
} }
}); });
} }
catch (UnauthorizedAccessException e) else
{ {
Logger.LogError(e.Message); Logger.LogInfo("File is too big to display. Showing error message");
AddTextBoxControl(Resources.Access_Denied_Exception_Message); AddTextBoxControl(Resources.Max_File_Size_Error.Replace("%1", (_settings.MaxFileSize / 1000).ToString(CultureInfo.CurrentCulture), StringComparison.InvariantCulture));
} }
catch (Exception e)
{
Logger.LogError(e.Message);
string errorMessage = Resources.Exception_Occurred;
errorMessage += e.Message;
errorMessage += "\n" + e.Source;
errorMessage += "\n" + e.StackTrace;
AddTextBoxControl(errorMessage);
}
this.Resize += FormResize;
} }
else catch (UnauthorizedAccessException e)
{ {
Logger.LogInfo("File is too big to display. Showing error message"); Logger.LogError(e.Message);
AddTextBoxControl(Resources.Max_File_Size_Error.Replace("%1", (_settings.MaxFileSize / 1000).ToString(CultureInfo.CurrentCulture), StringComparison.InvariantCulture)); AddTextBoxControl(Resources.Access_Denied_Exception_Message);
} }
catch (Exception e)
{
Logger.LogError(e.Message);
string errorMessage = Resources.Exception_Occurred;
errorMessage += e.Message;
errorMessage += "\n" + e.Source;
errorMessage += "\n" + e.StackTrace;
AddTextBoxControl(errorMessage);
}
this.Resize += FormResize;
} }
private async void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e) private async void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)