Skip to content

Continue CLI batches after malformed JSON input - #92

Draft
xingzhao-ji wants to merge 1 commit into
stefankoegl:masterfrom
xingzhao-ji:fix-cli-malformed-json-batches
Draft

xingzhao-ji wants to merge 1 commit into
stefankoegl:masterfrom
xingzhao-ji:fix-cli-malformed-json-batches

Conversation

@xingzhao-ji

Copy link
Copy Markdown

The CLI documents that remaining files are processed when a pointer cannot be resolved in one input. A malformed JSON file currently escapes that per-file handling: json.load() raises JSONDecodeError, the process prints a traceback, and valid files later in the batch are never processed.

For example, with invalid.json containing {"a": } and valid.json containing {"a": 1}, jsonpointer /a invalid.json valid.json currently prints no result. With this change, it reports the parse failure to stderr, prints 1 for the valid file, and exits with status 1.

This patch catches JSONDecodeError only around loading each document, reports the input filename and decoder error location, and continues with the remaining files. It retains status 1 for the batch and documents this case. Usage errors such as missing files retain their existing behavior.

Regression tests cover empty input, invalid syntax, extra top-level data, malformed input at every position in a three-file batch, and a batch containing both malformed JSON and an unresolved pointer.

Validation:

  • The three new test methods fail against the unchanged implementation, with seven failing cases.
  • The complete unittest/doctest suite passes: 56 tests on Python 3.12.14 and 3.13.5.
  • Ruff E/F checks pass for the source, tests, and CLI with the repository's 120-column limit; compileall and git diff --check pass.
  • The repository's coverage command passes (98% for the jsonpointer module; subprocess CLI execution is exercised by tests but is not included in that coverage figure).
  • Flake8 and the remaining CI Python versions were not run locally.

AI disclosure: This contribution was prepared by an OpenAI coding assistant. The reproductions and checks above were executed in its development environment.

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 97.917%. remained the same — xingzhao-ji:fix-cli-malformed-json-batches into stefankoegl:master

@stefankoegl

Copy link
Copy Markdown
Owner

Thanks for your contribution! Somehow I have the feeling that this behavior should be optional. What do you think about a --continue-on-error flag that controls it?

@Wkhanex Wkhanex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mixed-batch fix works for malformed JSON: I ran the real PR CLI with an invalid file at the beginning, middle and end of two valid files; all three runs retained both valid outputs and exited 1 without a traceback. A remaining adjacent malformed-input case still aborts the batch: a file containing bytes b'{"a":"\xff"}' raises UnicodeDecodeError, and the following valid file is never processed. Consider handling UnicodeDecodeError alongside JSONDecodeError, with the same file-labelled diagnostic and continuation behavior. AI-assisted review on Python 3.12; real CLI subprocesses used only synthetic temporary files.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants