Skip to content

One invalid chord entry in profiles.json makes KeybindingManager.InitializeAsync throw, although a syntactically corrupt file is tolerated #125

Description

@matt-edmondson

What's wrong

JsonKeybindingRepository.LoadAllProfilesInternalAsync (Keybinding/Services/JsonKeybindingRepository.cs:~99-120) catches only JsonException. For a corrupted file it returns an empty list.

The DTO-to-model conversion inside the same try builds Profile, Note and Chord objects. Those constructors throw ArgumentException when the JSON is well-formed but the content is invalid:

  • a chord with an empty notes array
  • a note string that is empty or whitespace
  • a profile with an empty id or name

That ArgumentException escapes LoadAllProfilesAsync, so KeybindingManager.InitializeAsync fails and the app can't start. SaveProfileAsync and DeleteProfileAsync reload the file first, so they throw the same exception and SaveAsync breaks too.

A hand-edited profile file is enough to hit this, and so is one written by an older version or partly migrated.

Reproduction (verified against the built library)

Write profiles.json in the data directory:

[{"id":"default","name":"Default","chords":{"file.save":{"notes":[]}}}]

Then run await new KeybindingManager(dir).InitializeAsync();.

  • Actual: ArgumentException: Chord must contain at least one note (Parameter 'notes') escapes InitializeAsync. If the file contains {not json instead, InitializeAsync succeeds, because the JsonException is swallowed.
  • Expected: the same tolerance as the corrupted-file path. The invalid binding or profile is skipped, the rest of the file loads, and nothing crashes.

Suggested fix / acceptance criteria

  • In the conversion, catch ArgumentException for each entry:
    • skip a chord entry whose notes are empty or invalid
    • skip a profile whose id or name is invalid
    • at minimum, catch ArgumentException alongside JsonException
  • Make sure a later SaveProfileAsync doesn't silently drop the skipped entries without telling the caller. For example, log or surface them.
  • Acceptance: the JSON above loads the default profile without a file.save binding, and InitializeAsync doesn't throw. Add a test for it next to the existing corrupted-file test.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions