From ee305e6041d88086cad2f025fe27f8bb2543c1ec Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 1 Oct 2023 16:20:45 +0200 Subject: [PATCH] backup: rework error reporting for subcommand --- cmd/restic/cmd_backup.go | 12 +++++------- internal/archiver/file_saver.go | 3 ++- internal/fs/fs_reader_command.go | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index 3e16bd801..a2b81a759 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -633,8 +633,6 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter wg.Go(func() error { return sc.Scan(cancelCtx, targets) }) } - snapshotCtx, cancelSnapshot := context.WithCancel(ctx) - arch := archiver.New(repo, targetFS, archiver.Options{ReadConcurrency: backupOptions.ReadConcurrency}) arch.SelectByName = selectByNameFilter arch.Select = selectFilter @@ -642,12 +640,13 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter success := true arch.Error = func(item string, err error) error { success = false + reterr := progressReporter.Error(item, err) // If we receive a fatal error during the execution of the snapshot, // we abort the snapshot. - if errors.IsFatal(err) { - cancelSnapshot() + if reterr == nil && errors.IsFatal(err) { + reterr = err } - return progressReporter.Error(item, err) + return reterr } arch.CompleteItem = progressReporter.CompleteItem arch.StartFile = progressReporter.StartFile @@ -674,8 +673,7 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter if !gopts.JSON { progressPrinter.V("start backup on %v", targets) } - _, id, err := arch.Snapshot(snapshotCtx, targets, snapshotOpts) - cancelSnapshot() + _, id, err := arch.Snapshot(ctx, targets, snapshotOpts) // cleanly shutdown all running goroutines cancel() diff --git a/internal/archiver/file_saver.go b/internal/archiver/file_saver.go index 0742c8b57..724f5e620 100644 --- a/internal/archiver/file_saver.go +++ b/internal/archiver/file_saver.go @@ -2,6 +2,7 @@ package archiver import ( "context" + "fmt" "io" "os" "sync" @@ -146,7 +147,7 @@ func (s *FileSaver) saveFile(ctx context.Context, chnker *chunker.Chunker, snPat panic("completed twice") } isCompleted = true - fnr.err = err + fnr.err = fmt.Errorf("failed to save %v: %w", target, err) fnr.node = nil fnr.stats = ItemStats{} finish(fnr) diff --git a/internal/fs/fs_reader_command.go b/internal/fs/fs_reader_command.go index 20d65a1ca..3830e5811 100644 --- a/internal/fs/fs_reader_command.go +++ b/internal/fs/fs_reader_command.go @@ -83,7 +83,7 @@ func (fp *CommandReader) wait() error { err := fp.cmd.Wait() if err != nil { // Use a fatal error to abort the snapshot. - return errors.Fatal(err.Error()) + return errors.Fatal(fmt.Errorf("command failed: %w", err).Error()) } return nil }