Bugfix #389 : Handle corrupt cache file

This commit is contained in:
Robert McRackan 2022-11-08 07:22:08 -05:00
parent 06f8d055fc
commit 7575736991
2 changed files with 17 additions and 16 deletions

View File

@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<Version>8.5.0.1</Version> <Version>8.5.1.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Octokit" Version="4.0.1" /> <PackageReference Include="Octokit" Version="4.0.1" />

View File

@ -24,23 +24,24 @@ namespace LibationFileManager
static FilePathCache() static FilePathCache()
{ {
// load json into memory. if file doesn't exist, nothing to do. save() will create if needed // load json into memory. if file doesn't exist, nothing to do. save() will create if needed
if (File.Exists(jsonFile)) if (!File.Exists(jsonFile))
{ return;
var list = JsonConvert.DeserializeObject<List<CacheEntry>>(File.ReadAllText(jsonFile));
// file exists but deser is null. this will never happen when file is healthy try
if (list is null) {
{ var list = JsonConvert.DeserializeObject<List<CacheEntry>>(File.ReadAllText(jsonFile));
lock (locker) if (list is null)
{ throw new NullReferenceException("File exists but deserialize is null. This will never happen when file is healthy.");
Serilog.Log.Logger.Error("Error deserializing file. Wrong format. Possibly corrupt. Deleting file. {@DebugInfo}", new { jsonFile });
File.Delete(jsonFile);
return;
}
}
cache = new Cache<CacheEntry>(list); cache = new Cache<CacheEntry>(list);
} }
catch (Exception ex)
{
Serilog.Log.Logger.Error(ex, "Error deserializing file. Wrong format. Possibly corrupt. Deleting file. {@DebugInfo}", new { jsonFile });
lock (locker)
File.Delete(jsonFile);
return;
}
} }
public static bool Exists(string id, FileType type) => GetFirstPath(id, type) is not null; public static bool Exists(string id, FileType type) => GetFirstPath(id, type) is not null;