Skip to content

Commit

Permalink
Potentially join errors in deferred function
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Feb 16, 2024
1 parent c2395c8 commit c8ca5ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions cmd/backup/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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()
Expand Down
22 changes: 11 additions & 11 deletions cmd/backup/run_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}()

Expand All @@ -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))
}
}()

Expand All @@ -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))
}
}()

Expand All @@ -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 {
Expand Down

0 comments on commit c8ca5ee

Please sign in to comment.