Skip to content

Commit

Permalink
[PT Run] Mitigate JSON Deserialization exception (#6295)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dsrivastavv committed Sep 8, 2020
1 parent 5065239 commit 52a06b5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PowerLauncherSettings : BasePTModuleSettings
public PowerLauncherSettings()
{
Properties = new PowerLauncherProperties();
Version = "1";
Version = "1.0";
Name = ModuleName;
}

Expand Down
5 changes: 5 additions & 0 deletions src/core/Microsoft.PowerToys.Settings.UI.Lib/SettingsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public static class SettingsUtils
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}"));
Expand Down
11 changes: 11 additions & 0 deletions src/modules/launcher/PowerLauncher/Helper/ErrorReporting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,6 +35,14 @@ private static void Report(Exception e, bool waitForClose)
}
}

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
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/modules/launcher/PowerLauncher/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
47 changes: 38 additions & 9 deletions src/modules/launcher/PowerLauncher/SettingsWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -34,6 +39,16 @@ public SettingsWatcher(Settings settings)
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);
Expand All @@ -44,13 +59,7 @@ public void OverloadSettings()
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);

Expand Down Expand Up @@ -99,10 +108,30 @@ public void OverloadSettings()
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);
}
}
}

Expand Down

0 comments on commit 52a06b5

Please sign in to comment.