Skip to content

Importing a JSON file that isn't a valid description array crashes Import with an unhandled JsonException #172

Description

@matt-edmondson

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions