From d6842115c5809d08ff0f399dce05f6cc5099aef9 Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Wed, 25 Oct 2023 11:30:34 +0200 Subject: [PATCH] no longer completely fail on one wrong entry U17 might have caused some journal bugs so don't fail immediately on one wrong entry, instead keep them in an Errors collection for later processing --- EDPlayerJournal/Entries/Entry.cs | 10 +++++++++- EDPlayerJournal/JournalException.cs | 1 + EDPlayerJournal/JournalFile.cs | 18 ++++++++++++++---- EDPlayerJournal/PlayerJournal.cs | 6 ++++++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/EDPlayerJournal/Entries/Entry.cs b/EDPlayerJournal/Entries/Entry.cs index bc2f09b..3a9b01b 100644 --- a/EDPlayerJournal/Entries/Entry.cs +++ b/EDPlayerJournal/Entries/Entry.cs @@ -64,7 +64,15 @@ public class Entry { public static Entry? Parse(string journalline) { using (JsonReader reader = new JsonTextReader(new StringReader(journalline))) { reader.DateParseHandling = DateParseHandling.None; - var json = JObject.Load(reader); + JObject? json = null; + try { + json = JObject.Load(reader); + } catch (Exception e) { + throw new InvalidJournalEntryException( + "invalid JSON journal entry: " + journalline, + e + ); + } if (json == null) { return null; } diff --git a/EDPlayerJournal/JournalException.cs b/EDPlayerJournal/JournalException.cs index 47e3d76..c24dbc7 100644 --- a/EDPlayerJournal/JournalException.cs +++ b/EDPlayerJournal/JournalException.cs @@ -6,6 +6,7 @@ public class InvalidJournalEntryException : Exception { public InvalidJournalEntryException() { } public InvalidJournalEntryException(string message) : base(message) { } + public InvalidJournalEntryException(string message, Exception inner) : base(message, inner) { } } /// diff --git a/EDPlayerJournal/JournalFile.cs b/EDPlayerJournal/JournalFile.cs index 438906f..bb54ff0 100644 --- a/EDPlayerJournal/JournalFile.cs +++ b/EDPlayerJournal/JournalFile.cs @@ -17,6 +17,11 @@ public class JournalFile : IComparable private static Regex update11regex = new Regex("Journal\\.([^\\.]+)\\.(\\d+).log"); private static string iso8601 = "yyyyMMddTHHmmss"; + /// + /// A public list of errors encountered while parsing the journal files + /// + public List Errors { get; private set; } = new List(); + public static bool VerifyFile(string path) { string filename = Path.GetFileName(path); @@ -125,14 +130,19 @@ public class JournalFile : IComparable } entries.Clear(); - foreach(var line in lines) { + Errors.Clear(); + foreach (var line in lines) { // Skip empty lines if (line.Trim().Length == 0) { continue; } - Entry? entry = Entry.Parse(line); - if (entry != null) { - entries.Add(entry); + try { + Entry? entry = Entry.Parse(line); + if (entry != null) { + entries.Add(entry); + } + } catch (Exception ex) { + Errors.Add(ex); } } } diff --git a/EDPlayerJournal/PlayerJournal.cs b/EDPlayerJournal/PlayerJournal.cs index 1a9bf92..fc0b5e6 100644 --- a/EDPlayerJournal/PlayerJournal.cs +++ b/EDPlayerJournal/PlayerJournal.cs @@ -22,6 +22,12 @@ public class PlayerJournal { ScanFiles(); } + public List AllErrors { + get { + return Files.SelectMany(x => x.Errors).ToList(); + } + } + public List Files { get { return journalfiles; } }