PowerToys/src/modules/launcher/PowerLauncher/Helper/ErrorReporting.cs
Divyansh Srivastava 52a06b5cdc
[PT Run] Mitigate JSON Deserialization exception (#6295)
* Add error reporting window on deserialisation error

* Add message box to show launcher errors

* Change report window to messagebox

* nit fix in error window

* Localized string for error window

* update messagebox interface

* Correct ShowMessageBox argument order
2020-09-08 10:04:17 -07:00

74 lines
2.2 KiB
C#

// Copyright (c) Microsoft Corporation
// 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.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Threading;
using NLog;
using Wox.Infrastructure;
using Wox.Infrastructure.Exception;
namespace PowerLauncher.Helper
{
public static class ErrorReporting
{
private static void Report(Exception e, bool waitForClose)
{
if (e != null)
{
var logger = LogManager.GetLogger("UnHandledException");
logger.Fatal(ExceptionFormatter.FormatException(e));
var reportWindow = new ReportWindow(e);
if (waitForClose)
{
reportWindow.ShowDialog();
}
else
{
reportWindow.Show();
}
}
}
public static void ShowMessageBox(string title, string message)
{
Application.Current.Dispatcher.Invoke(() =>
{
MessageBox.Show(message, title);
});
}
public static void UnhandledExceptionHandle(object sender, UnhandledExceptionEventArgs e)
{
// handle non-ui thread exceptions
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
Report((Exception)e?.ExceptionObject, true);
});
}
public static void DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// handle ui thread exceptions
Report(e?.Exception, false);
// prevent application exist, so the user can copy prompted error info
e.Handled = true;
}
public static string RuntimeInfo()
{
var info = $"\nVersion: {Constant.Version}" +
$"\nOS Version: {Environment.OSVersion.VersionString}" +
$"\nIntPtr Length: {IntPtr.Size}" +
$"\nx64: {Environment.Is64BitOperatingSystem}";
return info;
}
}
}