[PTRun]Refactor json storage save method for improved robustness and error handling (#31337)

This commit is contained in:
gokcekantarci
2024-02-27 17:39:06 +03:00
committed by GitHub
parent 4600bba7b6
commit 1a20e351ae

View File

@@ -130,11 +130,18 @@ namespace Wox.Infrastructure.Storage
try
{
string serialized = JsonSerializer.Serialize(_data, _serializerOptions);
File.WriteAllText(FilePath, serialized);
using var stream = new FileStream(FilePath, FileMode.Create, FileAccess.Write, FileShare.None);
using var writer = new StreamWriter(stream);
writer.Write(serialized);
_storageHelper.Close();
Log.Info($"Saving cached data at <{FilePath}>", GetType());
}
catch (UnauthorizedAccessException e)
{
Log.Exception($"Unauthorized access while saving data at <{FilePath}>. Check file permissions.", e, GetType());
}
catch (IOException e)
{
Log.Exception($"Error in saving data at <{FilePath}>", e, GetType());