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

@@ -32,7 +32,7 @@ func List(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error {
func ListJson(writer io.Writer, 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 marshall json: %s", err)
}
fmt.Println(string(jsonentries))

View File

@@ -16,7 +16,7 @@ func Print(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app.DbEn
if attr.File != "" {
fd, err := os.OpenFile(attr.File, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
return fmt.Errorf("failed to open file %s for writing: %w", attr.File, err)
}
defer fd.Close()
@@ -34,7 +34,7 @@ func Print(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app.DbEn
}
if err != nil {
return err
return fmt.Errorf("failed to write to file %s: %w", attr.File, err)
}
return nil
@@ -63,7 +63,7 @@ func Print(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app.DbEn
case "json":
jsonentry, err := json.Marshal(entry)
if err != nil {
return fmt.Errorf("json marshalling failure: %s", err)
return fmt.Errorf("failed to marshall json: %s", err)
}
fmt.Println(string(jsonentry))

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)