From 88f59fc2d6d40b71e74d8566117d496a9f949203 Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Sat, 3 Aug 2024 15:29:10 -0400 Subject: [PATCH] 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. --- doc/075_scripting.rst | 15 ++++++++++++++- internal/ui/backup/json.go | 16 ++++++++++------ internal/ui/backup/json_test.go | 4 ++-- internal/ui/restore/json.go | 14 +++++++++----- internal/ui/restore/json_test.go | 2 +- 5 files changed, 36 insertions(+), 15 deletions(-) diff --git a/doc/075_scripting.rst b/doc/075_scripting.rst index 87ae4fcf4..fa7fa1b6e 100644 --- a/doc/075_scripting.rst +++ b/doc/075_scripting.rst @@ -139,7 +139,7 @@ Error +----------------------+-------------------------------------------+ | ``message_type`` | Always "error" | +----------------------+-------------------------------------------+ -| ``error`` | Error message | +| ``error.message`` | Error message | +----------------------+-------------------------------------------+ | ``during`` | What restic was trying to do | +----------------------+-------------------------------------------+ @@ -539,6 +539,19 @@ Status |``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 ^^^^^^^^^^^^^^ diff --git a/internal/ui/backup/json.go b/internal/ui/backup/json.go index bb6685136..f4a76afd7 100644 --- a/internal/ui/backup/json.go +++ b/internal/ui/backup/json.go @@ -67,7 +67,7 @@ func (b *JSONProgress) Update(total, processed Counter, errors uint, currentFile func (b *JSONProgress) ScannerError(item string, err error) error { b.error(errorUpdate{ MessageType: "error", - Error: err.Error(), + Error: errorObject{err.Error()}, During: "scan", Item: item, }) @@ -78,7 +78,7 @@ func (b *JSONProgress) ScannerError(item string, err error) error { func (b *JSONProgress) Error(item string, err error) error { b.error(errorUpdate{ MessageType: "error", - Error: err.Error(), + Error: errorObject{err.Error()}, During: "archival", Item: item, }) @@ -205,11 +205,15 @@ type statusUpdate struct { CurrentFiles []string `json:"current_files,omitempty"` } +type errorObject struct { + Message string `json:"message"` +} + type errorUpdate struct { - MessageType string `json:"message_type"` // "error" - Error string `json:"error"` - During string `json:"during"` - Item string `json:"item"` + MessageType string `json:"message_type"` // "error" + Error errorObject `json:"error"` + During string `json:"during"` + Item string `json:"item"` } type verboseUpdate struct { diff --git a/internal/ui/backup/json_test.go b/internal/ui/backup/json_test.go index 4846279b3..b4872efd5 100644 --- a/internal/ui/backup/json_test.go +++ b/internal/ui/backup/json_test.go @@ -17,11 +17,11 @@ func createJSONProgress() (*ui.MockTerminal, ProgressPrinter) { func TestJSONError(t *testing.T) { term, printer := createJSONProgress() 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) { term, printer := createJSONProgress() 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) } diff --git a/internal/ui/restore/json.go b/internal/ui/restore/json.go index 4135dd667..72cc38a6e 100644 --- a/internal/ui/restore/json.go +++ b/internal/ui/restore/json.go @@ -48,7 +48,7 @@ func (t *jsonPrinter) Update(p State, duration time.Duration) { func (t *jsonPrinter) Error(item string, err error) error { t.error(errorUpdate{ MessageType: "error", - Error: err.Error(), + Error: errorObject{err.Error()}, During: "restore", Item: item, }) @@ -113,11 +113,15 @@ type statusUpdate struct { BytesSkipped uint64 `json:"bytes_skipped,omitempty"` } +type errorObject struct { + Message string `json:"message"` +} + type errorUpdate struct { - MessageType string `json:"message_type"` // "error" - Error string `json:"error"` - During string `json:"during"` - Item string `json:"item"` + MessageType string `json:"message_type"` // "error" + Error errorObject `json:"error"` + During string `json:"during"` + Item string `json:"item"` } type verboseUpdate struct { diff --git a/internal/ui/restore/json_test.go b/internal/ui/restore/json_test.go index 1e0f80a4f..917a48070 100644 --- a/internal/ui/restore/json_test.go +++ b/internal/ui/restore/json_test.go @@ -66,5 +66,5 @@ func TestJSONPrintCompleteItem(t *testing.T) { func TestJSONError(t *testing.T) { term, printer := createJSONProgress() 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) }