json: switch backup and restore errors from string to struct types

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.
This commit is contained in:
Michael Terry 2024-08-03 15:29:10 -04:00
parent a376323331
commit 88f59fc2d6
5 changed files with 36 additions and 15 deletions

View file

@ -139,7 +139,7 @@ Error
+----------------------+-------------------------------------------+ +----------------------+-------------------------------------------+
| ``message_type`` | Always "error" | | ``message_type`` | Always "error" |
+----------------------+-------------------------------------------+ +----------------------+-------------------------------------------+
| ``error`` | Error message | | ``error.message`` | Error message |
+----------------------+-------------------------------------------+ +----------------------+-------------------------------------------+
| ``during`` | What restic was trying to do | | ``during`` | What restic was trying to do |
+----------------------+-------------------------------------------+ +----------------------+-------------------------------------------+
@ -539,6 +539,19 @@ Status
|``bytes_skipped`` | Total size of skipped files | |``bytes_skipped`` | Total size of skipped files |
+----------------------+------------------------------------------------------------+ +----------------------+------------------------------------------------------------+
Error
^^^^^
+----------------------+-------------------------------------------+
| ``message_type`` | Always "error" |
+----------------------+-------------------------------------------+
| ``error.message`` | Error message |
+----------------------+-------------------------------------------+
| ``during`` | Always "restore" |
+----------------------+-------------------------------------------+
| ``item`` | Usually, the path of the problematic file |
+----------------------+-------------------------------------------+
Verbose Status Verbose Status
^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^

View file

@ -67,7 +67,7 @@ func (b *JSONProgress) Update(total, processed Counter, errors uint, currentFile
func (b *JSONProgress) ScannerError(item string, err error) error { func (b *JSONProgress) ScannerError(item string, err error) error {
b.error(errorUpdate{ b.error(errorUpdate{
MessageType: "error", MessageType: "error",
Error: err.Error(), Error: errorObject{err.Error()},
During: "scan", During: "scan",
Item: item, Item: item,
}) })
@ -78,7 +78,7 @@ func (b *JSONProgress) ScannerError(item string, err error) error {
func (b *JSONProgress) Error(item string, err error) error { func (b *JSONProgress) Error(item string, err error) error {
b.error(errorUpdate{ b.error(errorUpdate{
MessageType: "error", MessageType: "error",
Error: err.Error(), Error: errorObject{err.Error()},
During: "archival", During: "archival",
Item: item, Item: item,
}) })
@ -205,11 +205,15 @@ type statusUpdate struct {
CurrentFiles []string `json:"current_files,omitempty"` CurrentFiles []string `json:"current_files,omitempty"`
} }
type errorObject struct {
Message string `json:"message"`
}
type errorUpdate struct { type errorUpdate struct {
MessageType string `json:"message_type"` // "error" MessageType string `json:"message_type"` // "error"
Error string `json:"error"` Error errorObject `json:"error"`
During string `json:"during"` During string `json:"during"`
Item string `json:"item"` Item string `json:"item"`
} }
type verboseUpdate struct { type verboseUpdate struct {

View file

@ -17,11 +17,11 @@ func createJSONProgress() (*ui.MockTerminal, ProgressPrinter) {
func TestJSONError(t *testing.T) { func TestJSONError(t *testing.T) {
term, printer := createJSONProgress() term, printer := createJSONProgress()
test.Equals(t, printer.Error("/path", errors.New("error \"message\"")), nil) test.Equals(t, printer.Error("/path", errors.New("error \"message\"")), nil)
test.Equals(t, []string{"{\"message_type\":\"error\",\"error\":\"error \\\"message\\\"\",\"during\":\"archival\",\"item\":\"/path\"}\n"}, term.Errors) test.Equals(t, []string{"{\"message_type\":\"error\",\"error\":{\"message\":\"error \\\"message\\\"\"},\"during\":\"archival\",\"item\":\"/path\"}\n"}, term.Errors)
} }
func TestJSONScannerError(t *testing.T) { func TestJSONScannerError(t *testing.T) {
term, printer := createJSONProgress() term, printer := createJSONProgress()
test.Equals(t, printer.ScannerError("/path", errors.New("error \"message\"")), nil) test.Equals(t, printer.ScannerError("/path", errors.New("error \"message\"")), nil)
test.Equals(t, []string{"{\"message_type\":\"error\",\"error\":\"error \\\"message\\\"\",\"during\":\"scan\",\"item\":\"/path\"}\n"}, term.Errors) test.Equals(t, []string{"{\"message_type\":\"error\",\"error\":{\"message\":\"error \\\"message\\\"\"},\"during\":\"scan\",\"item\":\"/path\"}\n"}, term.Errors)
} }

View file

@ -48,7 +48,7 @@ func (t *jsonPrinter) Update(p State, duration time.Duration) {
func (t *jsonPrinter) Error(item string, err error) error { func (t *jsonPrinter) Error(item string, err error) error {
t.error(errorUpdate{ t.error(errorUpdate{
MessageType: "error", MessageType: "error",
Error: err.Error(), Error: errorObject{err.Error()},
During: "restore", During: "restore",
Item: item, Item: item,
}) })
@ -113,11 +113,15 @@ type statusUpdate struct {
BytesSkipped uint64 `json:"bytes_skipped,omitempty"` BytesSkipped uint64 `json:"bytes_skipped,omitempty"`
} }
type errorObject struct {
Message string `json:"message"`
}
type errorUpdate struct { type errorUpdate struct {
MessageType string `json:"message_type"` // "error" MessageType string `json:"message_type"` // "error"
Error string `json:"error"` Error errorObject `json:"error"`
During string `json:"during"` During string `json:"during"`
Item string `json:"item"` Item string `json:"item"`
} }
type verboseUpdate struct { type verboseUpdate struct {

View file

@ -66,5 +66,5 @@ func TestJSONPrintCompleteItem(t *testing.T) {
func TestJSONError(t *testing.T) { func TestJSONError(t *testing.T) {
term, printer := createJSONProgress() term, printer := createJSONProgress()
test.Equals(t, printer.Error("/path", errors.New("error \"message\"")), nil) test.Equals(t, printer.Error("/path", errors.New("error \"message\"")), nil)
test.Equals(t, []string{"{\"message_type\":\"error\",\"error\":\"error \\\"message\\\"\",\"during\":\"restore\",\"item\":\"/path\"}\n"}, term.Errors) test.Equals(t, []string{"{\"message_type\":\"error\",\"error\":{\"message\":\"error \\\"message\\\"\"},\"during\":\"restore\",\"item\":\"/path\"}\n"}, term.Errors)
} }