Skip to content

Commit

Permalink
Auto prepend caller when wrapping errors
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Feb 16, 2024
1 parent 29fd38d commit 98ce62d
Show file tree
Hide file tree
Showing 24 changed files with 295 additions and 208 deletions.
42 changes: 21 additions & 21 deletions cmd/backup/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ import (
"runtime"
"strings"

"github.com/klauspost/pgzip"

"github.com/klauspost/compress/zstd"
"github.com/klauspost/pgzip"
"github.com/offen/docker-volume-backup/internal/errwrap"
)

func createArchive(files []string, inputFilePath, outputFilePath string, compression string, compressionConcurrency int) error {
inputFilePath = stripTrailingSlashes(inputFilePath)
inputFilePath, outputFilePath, err := makeAbsolute(inputFilePath, outputFilePath)
if err != nil {
return fmt.Errorf("createArchive: error transposing given file paths: %w", err)
return errwrap.Wrap(err, "error transposing given file paths")
}
if err := os.MkdirAll(filepath.Dir(outputFilePath), 0755); err != nil {
return fmt.Errorf("createArchive: error creating output file path: %w", err)
return errwrap.Wrap(err, "error creating output file path")
}

if err := compress(files, outputFilePath, filepath.Dir(inputFilePath), compression, compressionConcurrency); err != nil {
return fmt.Errorf("createArchive: error creating archive: %w", err)
return errwrap.Wrap(err, "error creating archive")
}

return nil
Expand All @@ -58,35 +58,35 @@ func makeAbsolute(inputFilePath, outputFilePath string) (string, string, error)
func compress(paths []string, outFilePath, subPath string, algo string, concurrency int) error {
file, err := os.Create(outFilePath)
if err != nil {
return fmt.Errorf("compress: error creating out file: %w", err)
return errwrap.Wrap(err, "error creating out file")
}

prefix := path.Dir(outFilePath)
compressWriter, err := getCompressionWriter(file, algo, concurrency)
if err != nil {
return fmt.Errorf("compress: error getting compression writer: %w", err)
return errwrap.Wrap(err, "error getting compression writer")
}
tarWriter := tar.NewWriter(compressWriter)

for _, p := range paths {
if err := writeTarball(p, tarWriter, prefix); err != nil {
return fmt.Errorf("compress: error writing %s to archive: %w", p, err)
return errwrap.Wrap(err, fmt.Sprintf("error writing %s to archive", p))
}
}

err = tarWriter.Close()
if err != nil {
return fmt.Errorf("compress: error closing tar writer: %w", err)
return errwrap.Wrap(err, "error closing tar writer")
}

err = compressWriter.Close()
if err != nil {
return fmt.Errorf("compress: error closing compression writer: %w", err)
return errwrap.Wrap(err, "error closing compression writer")
}

err = file.Close()
if err != nil {
return fmt.Errorf("compress: error closing file: %w", err)
return errwrap.Wrap(err, "error closing file")
}

return nil
Expand All @@ -97,33 +97,33 @@ func getCompressionWriter(file *os.File, algo string, concurrency int) (io.Write
case "gz":
w, err := pgzip.NewWriterLevel(file, 5)
if err != nil {
return nil, fmt.Errorf("getCompressionWriter: gzip error: %w", err)
return nil, errwrap.Wrap(err, "gzip error")
}

if concurrency == 0 {
concurrency = runtime.GOMAXPROCS(0)
}

if err := w.SetConcurrency(1<<20, concurrency); err != nil {
return nil, fmt.Errorf("getCompressionWriter: error setting concurrency: %w", err)
return nil, errwrap.Wrap(err, "error setting concurrency")
}

return w, nil
case "zst":
compressWriter, err := zstd.NewWriter(file)
if err != nil {
return nil, fmt.Errorf("getCompressionWriter: zstd error: %w", err)
return nil, errwrap.Wrap(err, "zstd error")
}
return compressWriter, nil
default:
return nil, fmt.Errorf("getCompressionWriter: unsupported compression algorithm: %s", algo)
return nil, errwrap.Wrap(nil, fmt.Sprintf("unsupported compression algorithm: %s", algo))
}
}

func writeTarball(path string, tarWriter *tar.Writer, prefix string) error {
fileInfo, err := os.Lstat(path)
if err != nil {
return fmt.Errorf("writeTarball: error getting file infor for %s: %w", path, err)
return errwrap.Wrap(err, fmt.Sprintf("error getting file info for %s", path))
}

if fileInfo.Mode()&os.ModeSocket == os.ModeSocket {
Expand All @@ -134,19 +134,19 @@ func writeTarball(path string, tarWriter *tar.Writer, prefix string) error {
if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
var err error
if link, err = os.Readlink(path); err != nil {
return fmt.Errorf("writeTarball: error resolving symlink %s: %w", path, err)
return errwrap.Wrap(err, fmt.Sprintf("error resolving symlink %s", path))
}
}

header, err := tar.FileInfoHeader(fileInfo, link)
if err != nil {
return fmt.Errorf("writeTarball: error getting file info header: %w", err)
return errwrap.Wrap(err, "error getting file info header")
}
header.Name = strings.TrimPrefix(path, prefix)

err = tarWriter.WriteHeader(header)
if err != nil {
return fmt.Errorf("writeTarball: error writing file info header: %w", err)
return errwrap.Wrap(err, "error writing file info header")
}

if !fileInfo.Mode().IsRegular() {
Expand All @@ -155,13 +155,13 @@ func writeTarball(path string, tarWriter *tar.Writer, prefix string) error {

file, err := os.Open(path)
if err != nil {
return fmt.Errorf("writeTarball: error opening %s: %w", path, err)
return errwrap.Wrap(err, fmt.Sprintf("error opening %s", path))
}
defer file.Close()

_, err = io.Copy(tarWriter, file)
if err != nil {
return fmt.Errorf("writeTarball: error copying %s to tar writer: %w", path, err)
return errwrap.Wrap(err, fmt.Sprintf("error copying %s to tar writer", path))
}

return nil
Expand Down
21 changes: 11 additions & 10 deletions cmd/backup/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/signal"
"syscall"

"github.com/offen/docker-volume-backup/internal/errwrap"
"github.com/robfig/cron/v3"
)

Expand All @@ -31,12 +32,12 @@ func newCommand() *command {
func (c *command) runAsCommand() error {
configurations, err := sourceConfiguration(configStrategyEnv)
if err != nil {
return fmt.Errorf("runAsCommand: error loading env vars: %w", err)
return errwrap.Wrap(err, "error loading env vars")
}

for _, config := range configurations {
if err := runScript(config); err != nil {
return fmt.Errorf("runAsCommand: error running script: %w", err)
return errwrap.Wrap(err, "error running script")
}
}

Expand All @@ -59,12 +60,12 @@ func (c *command) runInForeground(opts foregroundOpts) error {
)

if err := c.schedule(configStrategyConfd); err != nil {
return fmt.Errorf("runInForeground: error scheduling: %w", err)
return errwrap.Wrap(err, "error scheduling")
}

if opts.profileCronExpression != "" {
if _, err := c.cr.AddFunc(opts.profileCronExpression, c.profile); err != nil {
return fmt.Errorf("runInForeground: error adding profiling job: %w", err)
return errwrap.Wrap(err, "error adding profiling job")
}
}

Expand All @@ -81,7 +82,7 @@ func (c *command) runInForeground(opts foregroundOpts) error {
return nil
case <-c.reload:
if err := c.schedule(configStrategyConfd); err != nil {
return fmt.Errorf("runInForeground: error reloading configuration: %w", err)
return errwrap.Wrap(err, "error reloading configuration")
}
}
}
Expand All @@ -96,7 +97,7 @@ func (c *command) schedule(strategy configStrategy) error {

configurations, err := sourceConfiguration(strategy)
if err != nil {
return fmt.Errorf("schedule: error sourcing configuration: %w", err)
return errwrap.Wrap(err, "error sourcing configuration")
}

for _, cfg := range configurations {
Expand All @@ -114,7 +115,7 @@ func (c *command) schedule(strategy configStrategy) error {
fmt.Sprintf(
"Unexpected error running schedule %s: %v",
config.BackupCronExpression,
err,
errwrap.Unwrap(err),
),
"error",
err,
Expand All @@ -123,7 +124,7 @@ func (c *command) schedule(strategy configStrategy) error {
})

if err != nil {
return fmt.Errorf("addJob: error adding schedule %s: %w", config.BackupCronExpression, err)
return errwrap.Wrap(err, fmt.Sprintf("error adding schedule %s", config.BackupCronExpression))
}
c.logger.Info(fmt.Sprintf("Successfully scheduled backup %s with expression %s", config.source, config.BackupCronExpression))
if ok := checkCronSchedule(config.BackupCronExpression); !ok {
Expand All @@ -132,7 +133,7 @@ func (c *command) schedule(strategy configStrategy) error {
)

if err != nil {
return fmt.Errorf("schedule: error scheduling: %w", err)
return errwrap.Wrap(err, "error scheduling")
}
c.schedules = append(c.schedules, id)
}
Expand All @@ -146,7 +147,7 @@ func (c *command) schedule(strategy configStrategy) error {
func (c *command) must(err error) {
if err != nil {
c.logger.Error(
fmt.Sprintf("Fatal error running command: %v", err),
fmt.Sprintf("Fatal error running command: %v", errwrap.Unwrap(err)),
"error",
err,
)
Expand Down
22 changes: 12 additions & 10 deletions cmd/backup/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"regexp"
"strconv"
"time"

"github.com/offen/docker-volume-backup/internal/errwrap"
)

// Config holds all configuration values that are expected to be set
Expand Down Expand Up @@ -92,7 +94,7 @@ func (c *CompressionType) Decode(v string) error {
*c = CompressionType(v)
return nil
default:
return fmt.Errorf("config: error decoding compression type %s", v)
return errwrap.Wrap(nil, fmt.Sprintf("error decoding compression type %s", v))
}
}

Expand All @@ -115,7 +117,7 @@ func (c *CertDecoder) Decode(v string) error {
block, _ := pem.Decode(content)
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return fmt.Errorf("config: error parsing certificate: %w", err)
return errwrap.Wrap(err, "error parsing certificate")
}
*c = CertDecoder{Cert: cert}
return nil
Expand All @@ -131,7 +133,7 @@ func (r *RegexpDecoder) Decode(v string) error {
}
re, err := regexp.Compile(v)
if err != nil {
return fmt.Errorf("config: error compiling given regexp `%s`: %w", v, err)
return errwrap.Wrap(err, fmt.Sprintf("error compiling given regexp `%s`", v))
}
*r = RegexpDecoder{Re: re}
return nil
Expand All @@ -143,10 +145,10 @@ type NaturalNumber int
func (n *NaturalNumber) Decode(v string) error {
asInt, err := strconv.Atoi(v)
if err != nil {
return fmt.Errorf("config: error converting %s to int", v)
return errwrap.Wrap(nil, fmt.Sprintf("error converting %s to int", v))
}
if asInt <= 0 {
return fmt.Errorf("config: expected a natural number, got %d", asInt)
return errwrap.Wrap(nil, fmt.Sprintf("expected a natural number, got %d", asInt))
}
*n = NaturalNumber(asInt)
return nil
Expand All @@ -162,10 +164,10 @@ type WholeNumber int
func (n *WholeNumber) Decode(v string) error {
asInt, err := strconv.Atoi(v)
if err != nil {
return fmt.Errorf("config: error converting %s to int", v)
return errwrap.Wrap(nil, fmt.Sprintf("error converting %s to int", v))
}
if asInt < 0 {
return fmt.Errorf("config: expected a whole, positive number, including zero. Got %d", asInt)
return errwrap.Wrap(nil, fmt.Sprintf("expected a whole, positive number, including zero. Got %d", asInt))
}
*n = WholeNumber(asInt)
return nil
Expand All @@ -191,12 +193,12 @@ func (c *Config) applyEnv() (func() error, error) {
for _, lookup := range lookups {
if !lookup.ok {
if err := os.Unsetenv(lookup.key); err != nil {
return fmt.Errorf("(*Config).applyEnv: error unsetting env var %s: %w", lookup.key, err)
return errwrap.Wrap(err, fmt.Sprintf("error unsetting env var %s", lookup.key))
}
continue
}
if err := os.Setenv(lookup.key, lookup.value); err != nil {
return fmt.Errorf("(*Config).applyEnv: error setting back env var %s: %w", lookup.key, err)
return errwrap.Wrap(err, fmt.Sprintf("error setting back env var %s", lookup.key))
}
}
return nil
Expand All @@ -206,7 +208,7 @@ func (c *Config) applyEnv() (func() error, error) {
current, ok := os.LookupEnv(key)
lookups = append(lookups, envVarLookup{ok: ok, key: key, value: current})
if err := os.Setenv(key, value); err != nil {
return unset, fmt.Errorf("(*Config).applyEnv: error setting env var: %w", err)
return unset, errwrap.Wrap(err, "error setting env var")
}
}
return unset, nil
Expand Down
17 changes: 9 additions & 8 deletions cmd/backup/config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"

"github.com/joho/godotenv"
"github.com/offen/docker-volume-backup/internal/errwrap"
"github.com/offen/envconfig"
)

Expand All @@ -33,11 +34,11 @@ func sourceConfiguration(strategy configStrategy) ([]*Config, error) {
if os.IsNotExist(err) {
return sourceConfiguration(configStrategyEnv)
}
return nil, fmt.Errorf("sourceConfiguration: error loading config files: %w", err)
return nil, errwrap.Wrap(err, "error loading config files")
}
return cs, nil
default:
return nil, fmt.Errorf("sourceConfiguration: received unknown config strategy: %v", strategy)
return nil, errwrap.Wrap(nil, fmt.Sprintf("received unknown config strategy: %v", strategy))
}
}

Expand Down Expand Up @@ -68,7 +69,7 @@ func loadConfig(lookup envProxy) (*Config, error) {

var c = &Config{}
if err := envconfig.Process("", c); err != nil {
return nil, fmt.Errorf("loadConfig: failed to process configuration values: %w", err)
return nil, errwrap.Wrap(err, "failed to process configuration values")
}

return c, nil
Expand All @@ -77,7 +78,7 @@ func loadConfig(lookup envProxy) (*Config, error) {
func loadConfigFromEnvVars() (*Config, error) {
c, err := loadConfig(os.LookupEnv)
if err != nil {
return nil, fmt.Errorf("loadEnvVars: error loading config from environment: %w", err)
return nil, errwrap.Wrap(err, "error loading config from environment")
}
c.source = "from environment"
return c, nil
Expand All @@ -89,7 +90,7 @@ func loadConfigsFromEnvFiles(directory string) ([]*Config, error) {
if os.IsNotExist(err) {
return nil, err
}
return nil, fmt.Errorf("loadEnvFiles: failed to read files from env directory: %w", err)
return nil, errwrap.Wrap(err, "failed to read files from env directory")
}

configs := []*Config{}
Expand All @@ -100,11 +101,11 @@ func loadConfigsFromEnvFiles(directory string) ([]*Config, error) {
p := filepath.Join(directory, item.Name())
f, err := os.ReadFile(p)
if err != nil {
return nil, fmt.Errorf("loadEnvFiles: error reading %s: %w", item.Name(), err)
return nil, errwrap.Wrap(err, fmt.Sprintf("error reading %s", item.Name()))
}
envFile, err := godotenv.Unmarshal(os.ExpandEnv(string(f)))
if err != nil {
return nil, fmt.Errorf("loadEnvFiles: error reading config file %s: %w", p, err)
return nil, errwrap.Wrap(err, fmt.Sprintf("error reading config file %s", p))
}
lookup := func(key string) (string, bool) {
val, ok := envFile[key]
Expand All @@ -115,7 +116,7 @@ func loadConfigsFromEnvFiles(directory string) ([]*Config, error) {
}
c, err := loadConfig(lookup)
if err != nil {
return nil, fmt.Errorf("loadEnvFiles: error loading config from file %s: %w", p, err)
return nil, errwrap.Wrap(err, fmt.Sprintf("error loading config from file %s", p))
}
c.source = item.Name()
c.additionalEnvVars = envFile
Expand Down
Loading

0 comments on commit 98ce62d

Please sign in to comment.