mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-11 05:52:19 +02:00
Catch Exceptions when settings.json is corrupted (#7553)
* added a json exception catch statement as a fail safe to create a fresh configuration file when settings is corrupted * change formatting * add issue link * add extra info in the comment
This commit is contained in:
@@ -69,11 +69,13 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
|||||||
where T : ISettingsConfig, new()
|
where T : ISettingsConfig, new()
|
||||||
{
|
{
|
||||||
if (SettingsExists(powertoy, fileName))
|
if (SettingsExists(powertoy, fileName))
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
// Given the file already exists, to deserialize the file and read it's content.
|
// Given the file already exists, to deserialize the file and read it's content.
|
||||||
T deserializedSettings = GetFile<T>(powertoy, fileName);
|
T deserializedSettings = GetFile<T>(powertoy, fileName);
|
||||||
|
|
||||||
// IF the file needs to be modified, to save the new configurations accordingly.
|
// If the file needs to be modified, to save the new configurations accordingly.
|
||||||
if (deserializedSettings.UpgradeSettingsConfiguration())
|
if (deserializedSettings.UpgradeSettingsConfiguration())
|
||||||
{
|
{
|
||||||
SaveSettings(deserializedSettings.ToJsonString(), powertoy, fileName);
|
SaveSettings(deserializedSettings.ToJsonString(), powertoy, fileName);
|
||||||
@@ -81,14 +83,21 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
|||||||
|
|
||||||
return deserializedSettings;
|
return deserializedSettings;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
// Catch json deserialization exceptions when the file is corrupt and has an invalid json.
|
||||||
|
// If there are any deserialization issues like in https://github.com/microsoft/PowerToys/issues/7500, log the error and create a new settings.json file.
|
||||||
|
// This is different from the case where we have trailing zeros following a valid json file, which we have handled by trimming the trailing zeros.
|
||||||
|
catch (JsonException ex)
|
||||||
{
|
{
|
||||||
// If the settings file does not exist, to create a new object with default parameters and save it to a newly created settings file.
|
Logger.LogError($"Exception encountered while loading {powertoy} settings.", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the settings file does not exist or if the file is corrupt, to create a new object with default parameters and save it to a newly created settings file.
|
||||||
T newSettingsItem = new T();
|
T newSettingsItem = new T();
|
||||||
SaveSettings(newSettingsItem.ToJsonString(), powertoy, fileName);
|
SaveSettings(newSettingsItem.ToJsonString(), powertoy, fileName);
|
||||||
return newSettingsItem;
|
return newSettingsItem;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Given the powerToy folder name and filename to be accessed, this function deserializes and returns the file.
|
// Given the powerToy folder name and filename to be accessed, this function deserializes and returns the file.
|
||||||
private T GetFile<T>(string powertoyFolderName = DefaultModuleName, string fileName = DefaultFileName)
|
private T GetFile<T>(string powertoyFolderName = DefaultModuleName, string fileName = DefaultFileName)
|
||||||
|
|||||||
Reference in New Issue
Block a user