From 1e24df626ac85d1dbef73249e028cefa52986a51 Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Mon, 21 Feb 2022 10:24:56 -0500 Subject: [PATCH] Add error recovery around FileLocations.json to handle file corruption --- FileManager/PersistentDictionary.cs | 4 ++-- LibationFileManager/FilePathCache.cs | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/FileManager/PersistentDictionary.cs b/FileManager/PersistentDictionary.cs index 963b51a4..53e87c9a 100644 --- a/FileManager/PersistentDictionary.cs +++ b/FileManager/PersistentDictionary.cs @@ -208,7 +208,7 @@ namespace FileManager { var msg = "Unrecoverable error. Settings file cannot be found"; var ex = new FileNotFoundException(msg, Filepath); - Serilog.Log.Logger.Error(msg, ex); + Serilog.Log.Logger.Error(ex, msg); throw ex; } @@ -226,7 +226,7 @@ namespace FileManager { var msg = "Unrecoverable error. Unable to read settings from Settings file"; var ex = new NullReferenceException(msg); - Serilog.Log.Logger.Error(msg, ex); + Serilog.Log.Logger.Error(ex, msg); throw ex; } diff --git a/LibationFileManager/FilePathCache.cs b/LibationFileManager/FilePathCache.cs index 43b75bbc..4359ef88 100644 --- a/LibationFileManager/FilePathCache.cs +++ b/LibationFileManager/FilePathCache.cs @@ -26,6 +26,18 @@ namespace LibationFileManager if (File.Exists(jsonFile)) { var list = JsonConvert.DeserializeObject>(File.ReadAllText(jsonFile)); + + // file exists but deser is null. this will never happen when file is healthy + if (list is null) + { + lock (locker) + { + Serilog.Log.Logger.Error("Error deserializing file. Wrong format. Possibly corrupt. Deleting file. {@DebugInfo}", new { jsonFile }); + File.Delete(jsonFile); + return; + } + } + cache = new Cache(list); } }