mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
[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
This commit is contained in:
committed by
GitHub
parent
5065239266
commit
52a06b5cdc
@@ -17,7 +17,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
|
||||
public PowerLauncherSettings()
|
||||
{
|
||||
Properties = new PowerLauncherProperties();
|
||||
Version = "1";
|
||||
Version = "1.0";
|
||||
Name = ModuleName;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,11 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
|
||||
private const string DefaultFileName = "settings.json";
|
||||
private const string DefaultModuleName = "";
|
||||
|
||||
public static void DeleteSettings(string powertoy, string fileName = DefaultFileName)
|
||||
{
|
||||
File.Delete(GetSettingsPath(powertoy, fileName));
|
||||
}
|
||||
|
||||
public static bool SettingsFolderExists(string powertoy)
|
||||
{
|
||||
return Directory.Exists(Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"));
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
// 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;
|
||||
@@ -32,6 +35,14 @@ namespace PowerLauncher.Helper
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -267,6 +267,24 @@ namespace PowerLauncher.Properties {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Settings will be reset to default and program will continue to function..
|
||||
/// </summary>
|
||||
public static string deseralization_error_message {
|
||||
get {
|
||||
return ResourceManager.GetString("deseralization_error_message", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Powertoys Run deserialization error.
|
||||
/// </summary>
|
||||
public static string deseralization_error_title {
|
||||
get {
|
||||
return ResourceManager.GetString("deseralization_error_title", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Disable.
|
||||
/// </summary>
|
||||
|
||||
@@ -462,4 +462,10 @@
|
||||
<data name="Title" xml:space="preserve">
|
||||
<value>Title</value>
|
||||
</data>
|
||||
<data name="deseralization_error_title" xml:space="preserve">
|
||||
<value>Powertoys Run deserialization error</value>
|
||||
</data>
|
||||
<data name="deseralization_error_message" xml:space="preserve">
|
||||
<value>Settings will be reset to default and program will continue to function.</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -5,13 +5,18 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Windows.Input;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Newtonsoft.Json;
|
||||
using PowerLauncher.Helper;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Infrastructure.Hotkey;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.UserSettings;
|
||||
using Wox.Plugin;
|
||||
using JsonException = System.Text.Json.JsonException;
|
||||
|
||||
namespace PowerLauncher
|
||||
{
|
||||
@@ -34,6 +39,16 @@ namespace PowerLauncher
|
||||
OverloadSettings();
|
||||
}
|
||||
|
||||
public static void CreateSettingsIfNotExists()
|
||||
{
|
||||
if (!SettingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
|
||||
{
|
||||
Log.Info("|SettingsWatcher.OverloadSettings|PT Run settings.json was missing, creating a new one");
|
||||
var defaultSettings = new PowerLauncherSettings();
|
||||
defaultSettings.Save();
|
||||
}
|
||||
}
|
||||
|
||||
public void OverloadSettings()
|
||||
{
|
||||
Monitor.Enter(_watcherSyncObject);
|
||||
@@ -44,13 +59,7 @@ namespace PowerLauncher
|
||||
try
|
||||
{
|
||||
retryCount++;
|
||||
if (!SettingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
|
||||
{
|
||||
Debug.WriteLine("PT Run settings.json was missing, creating a new one");
|
||||
|
||||
var defaultSettings = new PowerLauncherSettings();
|
||||
defaultSettings.Save();
|
||||
}
|
||||
CreateSettingsIfNotExists();
|
||||
|
||||
var overloadSettings = SettingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
|
||||
|
||||
@@ -99,10 +108,30 @@ namespace PowerLauncher
|
||||
if (retryCount > MaxRetries)
|
||||
{
|
||||
retry = false;
|
||||
Log.Exception($"|SettingsWatcher.OverloadSettings| Failed to Deserialize PowerToys settings, Retrying {e.Message}", e);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
Thread.Sleep(1000);
|
||||
Debug.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
catch (JsonException e)
|
||||
{
|
||||
if (retryCount > MaxRetries)
|
||||
{
|
||||
retry = false;
|
||||
Log.Exception($"|SettingsWatcher.OverloadSettings| Failed to Deserialize PowerToys settings, Creating new settings as file could be corrupted {e.Message}", e);
|
||||
|
||||
// Settings.json could possibly be corrupted. To mitigate this we delete the
|
||||
// current file and replace it with a correct json value.
|
||||
SettingsUtils.DeleteSettings(PowerLauncherSettings.ModuleName);
|
||||
CreateSettingsIfNotExists();
|
||||
ErrorReporting.ShowMessageBox(Properties.Resources.deseralization_error_title, Properties.Resources.deseralization_error_message);
|
||||
}
|
||||
else
|
||||
{
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user