Skip to content

Improve error handling in --json mode#4946

Merged
MichaelEischer merged 4 commits intorestic:masterfrom
mikix:json-errors
Aug 10, 2024
Merged

Improve error handling in --json mode#4946
MichaelEischer merged 4 commits intorestic:masterfrom
mikix:json-errors

Conversation

@mikix
Copy link
Copy Markdown
Contributor

@mikix mikix commented Jul 27, 2024

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

The main problem this solves are:

  1. The restore command does not provide JSON versions of its errors.
  2. The backup command's error output in --json mode does not actually show the error. Because the json package can't marshal an error.

This addresses both of those, as well as a few fixes for issues I found along the way:

  • The backup command was printing scanner errors to stderr, not stdout.
  • Added a ui.Terminal interface, to make it easier to test printing code.

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

Closes #4944
Closes #4945

Checklist

  • I have read the contribution guidelines.
  • I have enabled maintainer edits.
  • I have added tests for all code changes.
  • 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 27, 2024 23:46
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.

Thanks for the great PR! I have a few smaller comments, see below.

I'm a bit undecided how to handle the broken error message in the backup JSON output. It might be a good idea to maintain backwards compatibility with implementations that for example directly use the Go struct from the source code. Decoding the struct in Go would AFAICT result in a type error, when trying to decode {"error":"some message"} instead of {"error":{}}. So it might be better to use {"error":{"message":"some value"}} instead. That leads to a bit of not entirely necessary nesting, but would be fully backwards compatible and also provide room for adding an error code or similar. I'll have to think a bit about that.

[Edit]Parsing an old error message would fail: https://go.dev/play/p/qnXMavntl02 [/Edit]

@mikix mikix force-pushed the json-errors branch 2 times, most recently from e7c0346 to 33e8eb9 Compare July 31, 2024 00:40
@mikix
Copy link
Copy Markdown
Contributor Author

mikix commented Jul 31, 2024

I've updated this PR to remove the metadata-error change (now in a separate PR) and address all your comments, I believe. Please re-review! Thanks

@MichaelEischer
Copy link
Copy Markdown
Member

It will probably take a few days until I can take a look. I'll first have to sort out the regressions from restic 0.17.0.

@mikix
Copy link
Copy Markdown
Contributor Author

mikix commented Jul 31, 2024

No rush! I know I’m throwing a lot of PRs at ya, but there’s no time pressure from my end. Regressions come first! 😄

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.

The changes look good so far. The only thing left to sort out are the linter errors and the replacement type for the error field.

mikix added 3 commits August 3, 2024 15:07
Previously, an error JSON fragment would look like:
{"message_type": "error", "error": {}}

This is because encoding/json cannot marshal an error interface.
Instead, we now call .Error() to get the string value.
Previously, they were printed as freeform text.

This also adds a ui.Terminal interface to make writing
tests easier and also adds a few tests.
@mikix mikix force-pushed the json-errors branch 2 times, most recently from 8e68abc to 7b3a3d6 Compare August 3, 2024 19:57
@mikix
Copy link
Copy Markdown
Contributor Author

mikix commented Aug 3, 2024

OK should be ready again, and after noticing your comment about doc/075_scripting.rst in another PR, I updated that file here too.

This keeps backwards compatibility with the previous empty structs.
And maybe we'd want to put other fields into the inner struct later,
rather than the outer message.
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 10, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 10, 2024
@MichaelEischer MichaelEischer added this pull request to the merge queue Aug 10, 2024
Merged via the queue into restic:master with commit 0557128 Aug 10, 2024
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.

JSON backup errors don't contain an actual error message Restore errors with --json are not printed as JSON

2 participants