Skip to content

Format exit errors as JSON if requested#4952

Merged
MichaelEischer merged 3 commits intorestic:masterfrom
mikix:json-exit
Aug 15, 2024
Merged

Format exit errors as JSON if requested#4952
MichaelEischer merged 3 commits intorestic:masterfrom
mikix:json-exit

Conversation

@mikix
Copy link
Copy Markdown
Contributor

@mikix mikix commented Jul 28, 2024

What does this PR change? What problem does it solve?

When using --json, exit errors are now formatted as JSON instead of freeform text. And the exit code is included in the JSON blob, for the convenience of any consumers (so they don't have to wait for the exit code and then tie it together with the message).

While I was testing, I also noticed that the version command didn't use a message_type field in JSON mode - I figured I'd toss one in there for consistency with other messages. If that is undesired, I'm happy to take it out of this PR.

Was the change previously discussed in an issue or on the forum?

Closes #4948

Checklist

  • I have read the contribution guidelines.
  • I have enabled maintainer edits.
  • [ ] I have added tests for all code changes. (this felt hard to do with the exit call, unless I refactored things a bunch - but I did manually test this)
  • I have added documentation for relevant changes (in the manual).
  • There's a new file in changelog/unreleased/ that describes the changes for our users (see template).
  • I have run gofmt on the code in all commits.
  • All commit messages are formatted in the same style as the other commits in the repo.
  • I'm done! This pull request is ready for review.

@mikix mikix marked this pull request as ready for review July 28, 2024 13:16
@mikix mikix force-pushed the json-exit branch 5 times, most recently from f7343e7 to 194ef1a Compare August 3, 2024 20:32
Copy link
Copy Markdown
Member

@MichaelEischer MichaelEischer left a comment

Choose a reason for hiding this comment

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

Fine with me in general. I just have a few nitpicks, see below.

Output formats
--------------

Currently only the output on ``stdout`` is JSON formatted. Errors printed on ``stderr``
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why did you remove this remark? I think we still want to keep a remark that some stderr output is still text and not JSON.

Copy link
Copy Markdown
Contributor Author

@mikix mikix Aug 11, 2024

Choose a reason for hiding this comment

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

I removed it because I felt like the errors-as-JSON situation was so much better that it didn't deserve a big warning. But maybe I over-corrected. How about the current text:

Commands print their main JSON output on ``stdout``.
The generated JSON output uses one of the following two formats.

.. note::
    Not all messages and errors have been converted to JSON yet.
    Feel free to submit a pull request!

That note mirrors an earlier one in the doc about some commands not supporting --json at all.

(I also emphasized that some error messages print to stderr a few more places in the docs.)

Exit errors
-----------

Fatal errors will result in a final JSON message before the process exits.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It might be interesting to add a remark that JSON output cannot be guaranteed if parsing the CLI arguments failed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added this comment:

.. note::
    Some errors cannot be caught and reported this way,
    such as Go runtime errors or command line parsing errors.

Copy link
Copy Markdown
Member

@MichaelEischer MichaelEischer left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@MichaelEischer MichaelEischer added this pull request to the merge queue Aug 15, 2024
Merged via the queue into restic:master with commit 7462471 Aug 15, 2024
@mikix mikix deleted the json-exit branch August 15, 2024 20:34
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.

Fatal errors should be JSON formatted with --json

2 participants