Skip to content

Report errors of jsondiff and jsonpatch in one line - #271

Open
stefankoegl wants to merge 1 commit into
masterfrom
claude/cli-error-messages
Open

stefankoegl wants to merge 1 commit into
masterfrom
claude/cli-error-messages

Conversation

@stefankoegl

@stefankoegl stefankoegl commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

A patch that could not be applied, an invalid patch, invalid JSON, and errors reading or writing files all ended the command line programs with a Python traceback. They now print one line to stderr and exit with status 2:

$ jsonpatch doc.json patch.json
jsonpatch: error: can't remove a non-existent object 'zz'

$ jsondiff a.json broken.json
jsondiff: error: broken.json: Expecting property name enclosed in double quotes: line 2 column 1 (char 2)

This is the format and status argparse already uses for wrong arguments and files that cannot be opened.

Behaviour changes

  • Exit status 2 instead of 1 for these errors. This keeps them apart from status 1, with which jsondiff reports that the files differ (as diff does). Scripts that test for "non-zero" are unaffected; scripts that test jsonpatch for exactly 1 would need changing.
  • Messages for invalid JSON and invalid UTF-8 name the file, or <stdin>.
  • Handled: JsonPatchException, JsonPointerException, ValueError (JSON and encoding errors) and OSError. Anything else would be a bug and still shows a traceback. Ctrl-C still exits silently with status 1.

Internally, diff_files() and patch_files() now raise a ValueError that wraps the JSONDecodeError (as __cause__) in order to carry the file name.

Docs and tests

  • doc/commandline.rst describes the exit statuses.
  • New tests for each kind of error for both programs. Three existing tests looked for exception names in the traceback and now check the message instead.

python tests.py passes with coverage at 100%; flake8 and sphinx-build -W are clean (Python 3.13).

Touches jsonpatch_cli.py and tests.py in different places than #270, so the two should merge in either order.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PewzpjVeke4LTQ7Q88nyu6

A patch that could not be applied, an invalid patch, invalid JSON and
errors reading or writing files ended the programs with a traceback. They
now print 'PROGRAM: error: MESSAGE' to stderr and exit with status 2, as
they already do for wrong arguments and files that can't be opened.
Messages for invalid JSON and invalid UTF-8 name the file.

Status 2 keeps errors apart from status 1, with which jsondiff reports
that the files differ. Other exceptions, which would be bugs, still show
a traceback.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PewzpjVeke4LTQ7Q88nyu6
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — claude/cli-error-messages into master

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.

3 participants