mirror of
https://codeberg.org/scip/anydb.git
synced 2025-12-17 04:20:59 +01:00
better error handling
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user