[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:
Divyansh Srivastava
2020-09-08 10:04:17 -07:00
committed by GitHub
parent 5065239266
commit 52a06b5cdc
6 changed files with 79 additions and 10 deletions

View File

@@ -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);
}
}
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);
Thread.Sleep(1000);
Debug.WriteLine(e.Message);
// 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);
}
}
}