From 3c8896240fc86ff6e404d679f642e673f282bcb0 Mon Sep 17 00:00:00 2001 From: Pooya Azarpour Date: Wed, 29 Nov 2023 15:26:27 +0330 Subject: [PATCH] [CHANGE] Better handling error for json unmarshal when check `isRestoreSingleFile` --- restic/cli/restore.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/restic/cli/restore.go b/restic/cli/restore.go index 4a3356aad..912bab429 100644 --- a/restic/cli/restore.go +++ b/restic/cli/restore.go @@ -242,12 +242,27 @@ func (r *Restic) isRestoreSingleFile(log logr.Logger, snapshot dto.Snapshot) (bo stdOutLines := strings.Split(capturedStdOut, "\n") + if len(stdOutLines) == 0 { + err := fmt.Errorf("no list exist for snapshot %v", snapshot.ID) + log.Error(err, "the snapshot list is empty") + return false, err + } + + err := json.Unmarshal([]byte(stdOutLines[0]), &dto.Snapshot{}) + if err != nil { + return false, err + } + count := 0 - for _, fileJSON := range stdOutLines { + for i := 1; i < len(stdOutLines); i++ { + if len(stdOutLines[i]) == 0 { + continue + } + node := &fileNode{} - err := json.Unmarshal([]byte(fileJSON), node) + err := json.Unmarshal([]byte(stdOutLines[i]), node) if err != nil { - continue + return false, err } if node.Type == "file" { count++