backup: rework error reporting for subcommand

This commit is contained in:
Michael Eischer 2023-10-01 16:20:45 +02:00
parent 8bceb8e359
commit ee305e6041
3 changed files with 8 additions and 9 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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
}