better error handling

This commit is contained in:
2024-12-19 10:55:39 +01:00
parent 1b9ec48396
commit 97837076a3
6 changed files with 29 additions and 29 deletions

View File

@@ -12,7 +12,7 @@ import (
func WriteFile(attr *app.DbAttr, conf *cfg.Config, entries app.DbEntries) error {
jsonentries, err := json.Marshal(entries)
if err != nil {
return fmt.Errorf("json marshalling failure: %s", err)
return fmt.Errorf("failed to marshall json: %w", err)
}
if attr.File == "-" {
@@ -20,11 +20,11 @@ func WriteFile(attr *app.DbAttr, conf *cfg.Config, entries app.DbEntries) error
} else {
fd, err := os.OpenFile(attr.File, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
return fmt.Errorf("failed to open file %s for writing: %w", attr.File, err)
}
if _, err := fd.Write(jsonentries); err != nil {
return err
return fmt.Errorf("failed writing to file %s: %w", attr.File, err)
}
fmt.Printf("database contents exported to %s\n", attr.File)