diff --git a/cmd/backup/exec.go b/cmd/backup/exec.go index 1fa09976..d001469c 100644 --- a/cmd/backup/exec.go +++ b/cmd/backup/exec.go @@ -9,6 +9,7 @@ package main import ( "bytes" "context" + "errors" "fmt" "io" "os" @@ -190,13 +191,12 @@ func (s *script) withLabeledCommands(step lifecyclePhase, cb func() error) func( } return func() (err error) { if err = s.runLabeledCommands(fmt.Sprintf("docker-volume-backup.%s-pre", step)); err != nil { - err = fmt.Errorf("withLabeledCommands: %s: error running pre commands: %w", step, err) + err = fmt.Errorf("(*script).withLabeledCommands: %s: error running pre commands: %w", step, err) return } defer func() { - derr := s.runLabeledCommands(fmt.Sprintf("docker-volume-backup.%s-post", step)) - if err == nil && derr != nil { - err = derr + if derr := s.runLabeledCommands(fmt.Sprintf("docker-volume-backup.%s-post", step)); derr != nil { + err = errors.Join(err, fmt.Errorf("(*script).withLabeledCommands: error running %s-post commands: %w", step, derr)) } }() err = cb() diff --git a/cmd/backup/run_script.go b/cmd/backup/run_script.go index 9794b334..e734f103 100644 --- a/cmd/backup/run_script.go +++ b/cmd/backup/run_script.go @@ -3,7 +3,10 @@ package main -import "fmt" +import ( + "errors" + "fmt" +) // runScript instantiates a new script object and orchestrates a backup run. // To ensure it runs mutually exclusive a global file lock is acquired before @@ -12,7 +15,7 @@ import "fmt" func runScript(c *Config) (err error) { defer func() { if derr := recover(); derr != nil { - err = fmt.Errorf("runScript: unexpected panic running script: %v", err) + err = fmt.Errorf("runScript: unexpected panic running script: %v", derr) } }() @@ -24,9 +27,8 @@ func runScript(c *Config) (err error) { return } defer func() { - derr := unlock() - if err == nil && derr != nil { - err = fmt.Errorf("runScript: error releasing file lock: %w", derr) + if derr := unlock(); derr != nil { + err = errors.Join(err, fmt.Errorf("runScript: error releasing file lock: %w", derr)) } }() @@ -35,9 +37,8 @@ func runScript(c *Config) (err error) { return fmt.Errorf("runScript: error applying env: %w", err) } defer func() { - derr := unset() - if err == nil && derr != nil { - err = fmt.Errorf("runScript: error unsetting environment variables: %w", derr) + if derr := unset(); derr != nil { + err = errors.Join(err, fmt.Errorf("runScript: error unsetting environment variables: %w", derr)) } }() @@ -54,9 +55,8 @@ func runScript(c *Config) (err error) { // should happen as soon as possible (i.e. before uploading backups or // similar). defer func() { - derr := restartContainersAndServices() - if err == nil { - err = derr + if derr := restartContainersAndServices(); derr != nil { + err = errors.Join(err, fmt.Errorf("runScript: error restarting containers and services: %w", derr)) } }() if err != nil {