What's wrong
Import.ImportJson (ImageDescriber/Verbs/Import.cs:144-148) calls JsonSerializer.Deserialize<List<ImageDescription>> and catches no exceptions. Neither of its callers does either: LoadEntries (Import.cs:74-86) and Run (Import.cs:47-72).
As a result, any JSON file that fails to deserialize ends the process with a stack trace. That includes a truncated file, an object at the root instead of an array, a string where a number is expected, and a date that won't parse.
The CSV path already handles malformed input. ParseCsv catches FormatException, OverflowException and ArgumentException for each row, prints Skipping line N: ... and carries on (Import.cs:200-211).
Reproduction (verified on .NET 10, Linux)
echo '{"not":"array"}' > bad.json
ImageDescriber Import -i bad.json
Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to
System.Collections.Generic.List`1[ktsu.ImageDescriber.ImageDescription]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
A file cut off mid-write, or a hand-edited file with a trailing comma, fails the same way. When Import is started from the interactive menu (the default verb), the crash also ends the menu session.
Why it matters
Import is how users move a database to another machine or restore an export. A bad file should produce a one-line error and leave the store untouched.
#171 mentions in passing that ImportJson doesn't catch the FileName validation exception. This issue is the general malformed-input case, which fixing #171 would not cover.
Suggested fix
Acceptance criteria
- Importing
{"not":"array"}, a truncated JSON array, or an entry with "FileSizeBytes":"abc" prints an error naming the file and does not throw.
persistent_state.json is unchanged afterwards.
- A test in
ImageDescriber.Test/ImportTests.cs covers the error path.
What's wrong
Import.ImportJson(ImageDescriber/Verbs/Import.cs:144-148) callsJsonSerializer.Deserialize<List<ImageDescription>>and catches no exceptions. Neither of its callers does either:LoadEntries(Import.cs:74-86) andRun(Import.cs:47-72).As a result, any JSON file that fails to deserialize ends the process with a stack trace. That includes a truncated file, an object at the root instead of an array, a string where a number is expected, and a date that won't parse.
The CSV path already handles malformed input.
ParseCsvcatchesFormatException,OverflowExceptionandArgumentExceptionfor each row, printsSkipping line N: ...and carries on (Import.cs:200-211).Reproduction (verified on .NET 10, Linux)
A file cut off mid-write, or a hand-edited file with a trailing comma, fails the same way. When Import is started from the interactive menu (the default verb), the crash also ends the menu session.
Why it matters
Import is how users move a database to another machine or restore an export. A bad file should produce a one-line error and leave the store untouched.
#171 mentions in passing that
ImportJsondoesn't catch theFileNamevalidation exception. This issue is the general malformed-input case, which fixing #171 would not cover.Suggested fix
ImportJson, catchJsonExceptionfrom the deserialize call, plusArgumentExceptionfor semantic-type validation. On failure, printError: <file> is not a valid ImageDescriber JSON export: <message>and returnnullso thatRunexits beforeProgram.Settings.Save().Acceptance criteria
{"not":"array"}, a truncated JSON array, or an entry with"FileSizeBytes":"abc"prints an error naming the file and does not throw.persistent_state.jsonis unchanged afterwards.ImageDescriber.Test/ImportTests.cscovers the error path.