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 2bd416a commit df7107f
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 137 deletions.
38 changes: 19 additions & 19 deletions cmd/backup/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ func createArchive(files []string, inputFilePath, outputFilePath string, compres
inputFilePath = stripTrailingSlashes(inputFilePath)
inputFilePath, outputFilePath, err := makeAbsolute(inputFilePath, outputFilePath)
if err != nil {
return fmt.Errorf("createArchive: error transposing given file paths: %w", err)
return wrapError(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 wrapError(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 wrapError(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 wrapError(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 wrapError(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 wrapError(err, fmt.Sprintln("error writing %s to archive", p))

Check failure on line 73 in cmd/backup/archive.go

View workflow job for this annotation

GitHub Actions / lint

printf: fmt.Sprintln call has possible Printf formatting directive %s (govet)
}
}

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

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

err = file.Close()
if err != nil {
return fmt.Errorf("compress: error closing file: %w", err)
return wrapError(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, wrapError(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, wrapError(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, wrapError(err, "zstd error")
}
return compressWriter, nil
default:
return nil, fmt.Errorf("getCompressionWriter: unsupported compression algorithm: %s", algo)
return nil, wrapError(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 wrapError(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 wrapError(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 wrapError(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 wrapError(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 wrapError(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 wrapError(err, fmt.Sprintf("error copying %s to tar writer", path))
}

return nil
Expand Down
16 changes: 8 additions & 8 deletions cmd/backup/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,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 wrapError(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 wrapError(err, "error running script")
}
}

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

if err := c.schedule(configStrategyConfd); err != nil {
return fmt.Errorf("runInForeground: error scheduling: %w", err)
return wrapError(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 wrapError(err, "error adding profiling job")
}
}

Expand All @@ -81,7 +81,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 wrapError(err, "error reloading configuration")
}
}
}
Expand All @@ -96,7 +96,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 wrapError(err, "error sourcing configuration")
}

for _, cfg := range configurations {
Expand All @@ -123,7 +123,7 @@ func (c *command) schedule(strategy configStrategy) error {
})

if err != nil {
return fmt.Errorf("schedule: error adding schedule %s: %w", config.BackupCronExpression, err)
return wrapError(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 +132,7 @@ func (c *command) schedule(strategy configStrategy) error {
)

if err != nil {
return fmt.Errorf("schedule: error scheduling: %w", err)
return wrapError(err, "error scheduling")
}
c.schedules = append(c.schedules, id)
}
Expand Down
20 changes: 10 additions & 10 deletions cmd/backup/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,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 wrapError(nil, fmt.Sprintf("error decoding compression type %s", v))
}
}

Expand All @@ -115,7 +115,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 wrapError(err, "error parsing certificate")
}
*c = CertDecoder{Cert: cert}
return nil
Expand All @@ -131,7 +131,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 wrapError(err, fmt.Sprintf("error compiling given regexp `%s`", v))
}
*r = RegexpDecoder{Re: re}
return nil
Expand All @@ -143,10 +143,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 wrapError(nil, fmt.Sprintf("error converting %s to int", v))
}
if asInt <= 0 {
return fmt.Errorf("config: expected a natural number, got %d", asInt)
return wrapError(nil, fmt.Sprintf("expected a natural number, got %d", asInt))
}
*n = NaturalNumber(asInt)
return nil
Expand All @@ -162,10 +162,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 wrapError(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 wrapError(nil, fmt.Sprintf("expected a whole, positive number, including zero. Got %d", asInt))
}
*n = WholeNumber(asInt)
return nil
Expand All @@ -191,12 +191,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 wrapError(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 wrapError(err, fmt.Sprintf("error setting back env var %s", lookup.key))
}
}
return nil
Expand All @@ -206,7 +206,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, wrapError(err, "error setting env var")
}
}
return unset, nil
Expand Down
16 changes: 8 additions & 8 deletions cmd/backup/config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,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, wrapError(err, "error loading config files")
}
return cs, nil
default:
return nil, fmt.Errorf("sourceConfiguration: received unknown config strategy: %v", strategy)
return nil, wrapError(nil, fmt.Sprintf("received unknown config strategy: %v", strategy))
}
}

Expand Down Expand Up @@ -68,7 +68,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, wrapError(err, "failed to process configuration values")
}

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

configs := []*Config{}
Expand All @@ -100,11 +100,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("loadConfigsFromEnvFiles: error reading %s: %w", item.Name(), err)
return nil, wrapError(err, fmt.Sprintf("error reading %s", item.Name()))
}
envFile, err := godotenv.Unmarshal(os.ExpandEnv(string(f)))
if err != nil {
return nil, fmt.Errorf("loadConfigsFromEnvFiles: error reading config file %s: %w", p, err)
return nil, wrapError(err, fmt.Sprintf("error reading config file %s", p))
}
lookup := func(key string) (string, bool) {
val, ok := envFile[key]
Expand All @@ -115,7 +115,7 @@ func loadConfigsFromEnvFiles(directory string) ([]*Config, error) {
}
c, err := loadConfig(lookup)
if err != nil {
return nil, fmt.Errorf("loadConfigsFromEnvFiles: error loading config from file %s: %w", p, err)
return nil, wrapError(err, fmt.Sprintf("error loading config from file %s", p))
}
c.source = item.Name()
c.additionalEnvVars = envFile
Expand Down
5 changes: 2 additions & 3 deletions cmd/backup/copy_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package main

import (
"fmt"
"os"
"path"

Expand All @@ -16,7 +15,7 @@ import (
func (s *script) copyArchive() error {
_, name := path.Split(s.file)
if stat, err := os.Stat(s.file); err != nil {
return fmt.Errorf("copyArchive: unable to stat backup file: %w", err)
return wrapError(err, "unable to stat backup file")
} else {
size := stat.Size()
s.stats.BackupFile = BackupFileStats{
Expand All @@ -34,7 +33,7 @@ func (s *script) copyArchive() error {
})
}
if err := eg.Wait(); err != nil {
return fmt.Errorf("copyArchive: error copying archive: %w", err)
return wrapError(err, "error copying archive")
}

return nil
Expand Down
Loading

0 comments on commit df7107f

Please sign in to comment.