From a59a0af2c05f5d402003876a8991f8a75e62c5f3 Mon Sep 17 00:00:00 2001 From: Raymonf Date: Sat, 5 Nov 2022 14:21:12 -0400 Subject: [PATCH] very minor error handling for bulk imports --- WTT/WTT/Exceptions/UnhandledTomlError.cs | 18 +++++++++++ WTT/WTT/TableImporter.cs | 41 +++++++++++++++++------- 2 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 WTT/WTT/Exceptions/UnhandledTomlError.cs diff --git a/WTT/WTT/Exceptions/UnhandledTomlError.cs b/WTT/WTT/Exceptions/UnhandledTomlError.cs new file mode 100644 index 0000000..2ad4bae --- /dev/null +++ b/WTT/WTT/Exceptions/UnhandledTomlError.cs @@ -0,0 +1,18 @@ +namespace WhackTranslationTool.Exceptions; + +public class UnhandledTomlError : Exception +{ + public UnhandledTomlError() + { + } + + public UnhandledTomlError(string message) + : base(message) + { + } + + public UnhandledTomlError(string message, Exception inner) + : base(message, inner) + { + } +} diff --git a/WTT/WTT/TableImporter.cs b/WTT/WTT/TableImporter.cs index bdba077..ec4099d 100644 --- a/WTT/WTT/TableImporter.cs +++ b/WTT/WTT/TableImporter.cs @@ -33,21 +33,40 @@ public class TableImporter private void ReadStrings(string tomlPath) { - using var reader = File.OpenText(tomlPath); - TomlTable table; - try + TomlTable? table = null; + + var read = false; + while (!read) { - table = TOML.Parse(reader); - } - catch (TomlParseException ex) - { - Console.WriteLine($"*** ERROR: Failed to parse {tomlPath}: {ex.Message}"); - foreach (var error in ex.SyntaxErrors) + try { - Console.WriteLine($"* Syntax error at line {error.Line}: {error.Message}"); + using var reader = File.OpenText(tomlPath); + table = TOML.Parse(reader); + read = true; + } + catch (TomlParseException ex) + { + Console.WriteLine($"*** ERROR: Failed to parse {tomlPath}: {ex.Message}"); + foreach (var error in ex.SyntaxErrors) + { + Console.WriteLine($"* Syntax error at line {error.Line}: {error.Message}"); + } + + // prompt to retry so I don't have to keep restarting this thing + Console.Write("Try again (Y/n)? "); + var response = Console.ReadLine()!.Trim().ToLowerInvariant(); + if (response is not ("" or "y" or "yes")) + { + break; + } } - throw; } + + if (table == null) + { + throw new UnhandledTomlError("TOML error was not handled"); + } + foreach (var (key, value) in table.RawTable) { if (string.IsNullOrEmpty(key))