From fcae0bd4dfd7fb435a9398851d6a753a16ca07af Mon Sep 17 00:00:00 2001 From: lew Date: Wed, 17 Dec 2025 14:49:24 +0000 Subject: [PATCH] chore(cmd): improves error messaging across the board --- cmd/del-db.go | 32 +++++++++++--------------------- cmd/dump.go | 14 +++++++------- cmd/list-dbs.go | 2 +- cmd/list.go | 12 ++++++------ cmd/restore.go | 18 +++++++++--------- cmd/shared.go | 6 +++--- 6 files changed, 37 insertions(+), 47 deletions(-) diff --git a/cmd/del-db.go b/cmd/del-db.go index 94cc801..c89399f 100644 --- a/cmd/del-db.go +++ b/cmd/del-db.go @@ -24,7 +24,6 @@ package cmd import ( "errors" "fmt" - "path/filepath" "strings" "github.com/spf13/cobra" @@ -46,47 +45,38 @@ func delDb(cmd *cobra.Command, args []string) error { var notFound errNotFound path, err := store.FindStore(args[0]) if errors.As(err, ¬Found) { - fmt.Fprintf(os.Stderr, "%q does not exist, %s\n", args[0], err.Error()) - os.Exit(1) + return fmt.Errorf("cannot delete-db '%s': %v", args[0], err) } if err != nil { - fmt.Fprintf(os.Stderr, "unexpected error: %s", err.Error()) - os.Exit(1) - } - - var confirm string - home, err := os.UserHomeDir() - nicepath := path - if err == nil && strings.HasPrefix(path, home) { - nicepath = filepath.Join("~", strings.TrimPrefix(nicepath, home)) + return fmt.Errorf("cannot delete-db '%s': %v", args[0], err) } force, err := cmd.Flags().GetBool("force") if err != nil { - return err + return fmt.Errorf("cannot delete-db '%s': %v", args[0], err) } if force { - return executeDeletion(path, nicepath) + return executeDeletion(path) } - message := fmt.Sprintf("Are you sure you want to delete '%s'? (y/n)", nicepath) + message := fmt.Sprintf("delete-db '%s': are you sure? (y/n)", args[0]) fmt.Println(message) + + var confirm string if _, err := fmt.Scanln(&confirm); err != nil { - return err + return fmt.Errorf("cannot delete-db '%s': %v", args[0], err) } if strings.ToLower(confirm) == "y" { - return executeDeletion(path, nicepath) + return executeDeletion(path) } - fmt.Fprintf(os.Stderr, "Did not delete %q\n", nicepath) return nil } -func executeDeletion(path, nicepath string) error { +func executeDeletion(path string) error { if err := os.RemoveAll(path); err != nil { - return err + return fmt.Errorf("cannot delete-db '%s': %v", path, err) } - fmt.Fprintf(os.Stderr, "Deleted %q\n", nicepath) return nil } diff --git a/cmd/dump.go b/cmd/dump.go index 72b40cd..2661028 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -35,12 +35,12 @@ func dump(cmd *cobra.Command, args []string) error { rawArg := args[0] dbName, err := store.parseDB(rawArg, false) if err != nil { - return err + return fmt.Errorf("cannot dump '%s': %v", rawArg, err) } if _, err := store.FindStore(dbName); err != nil { var notFound errNotFound if errors.As(err, ¬Found) { - return fmt.Errorf("%q does not exist, %s", rawArg, err.Error()) + return fmt.Errorf("cannot dump '%s': %v", rawArg, err) } return err } @@ -49,12 +49,12 @@ func dump(cmd *cobra.Command, args []string) error { mode, err := cmd.Flags().GetString("encoding") if err != nil { - return err + return fmt.Errorf("cannot dump '%s': %v", args[0], err) } switch mode { case "auto", "base64", "text": default: - return fmt.Errorf("unsupported encoding %q", mode) + return fmt.Errorf("cannot dump '%s': unsupported encoding '%s'", args[0], mode) } includeSecret, err := cmd.Flags().GetBool("secret") @@ -94,7 +94,7 @@ func dump(cmd *cobra.Command, args []string) error { encodeBase64(&entry, v) case "text": if err := encodeText(&entry, key, v); err != nil { - return err + return fmt.Errorf("cannot dump '%s': %v", args[0], err) } case "auto": if utf8.Valid(v) { @@ -106,12 +106,12 @@ func dump(cmd *cobra.Command, args []string) error { } payload, err := json.Marshal(entry) if err != nil { - return err + return fmt.Errorf("cannot dump '%s': %v", args[0], err) } fmt.Fprintln(cmd.OutOrStdout(), string(payload)) return nil }); err != nil { - return err + return fmt.Errorf("cannot dump '%s': %v", args[0], err) } } return nil diff --git a/cmd/list-dbs.go b/cmd/list-dbs.go index 2eff0a7..d31ea6b 100644 --- a/cmd/list-dbs.go +++ b/cmd/list-dbs.go @@ -40,7 +40,7 @@ func listDbs(cmd *cobra.Command, args []string) error { store := &Store{} dbs, err := store.AllStores() if err != nil { - return err + return fmt.Errorf("cannot list-dbs: %v", err) } for _, db := range dbs { fmt.Println("@" + db) diff --git a/cmd/list.go b/cmd/list.go index eec7fa5..015130e 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -46,26 +46,26 @@ func list(cmd *cobra.Command, args []string) error { rawArg := args[0] dbName, err := store.parseDB(rawArg, false) if err != nil { - return err + return fmt.Errorf("cannot ls '%s': %v", args[0], err) } if _, err := store.FindStore(dbName); err != nil { var notFound errNotFound if errors.As(err, ¬Found) { - return fmt.Errorf("%q does not exist, %s", rawArg, err.Error()) + return fmt.Errorf("cannot ls '%s': No such DB", args[0]) } - return err + return fmt.Errorf("cannot ls '%s': %v", args[0], err) } targetDB = "@" + dbName } flags, err := enrichFlags() if err != nil { - return err + return fmt.Errorf("cannot ls '%s': %v", targetDB, err) } columnKinds, err := requireColumns(flags) if err != nil { - return err + return fmt.Errorf("cannot ls '%s': %v", targetDB, err) } output := cmd.OutOrStdout() @@ -112,7 +112,7 @@ func list(cmd *cobra.Command, args []string) error { valueBuf = append(valueBuf[:0], v...) return nil }); err != nil { - return err + return fmt.Errorf("cannot ls '%s': %v", targetDB, err) } valueStr = store.FormatBytes(flags.binary, valueBuf) } diff --git a/cmd/restore.go b/cmd/restore.go index 671b136..0caa804 100644 --- a/cmd/restore.go +++ b/cmd/restore.go @@ -28,14 +28,14 @@ func restore(cmd *cobra.Command, args []string) error { if len(args) == 1 { parsed, err := store.parseDB(args[0], false) if err != nil { - return err + return fmt.Errorf("cannot restore '%s': %v", args[0], err) } dbName = parsed } reader, closer, err := restoreInput(cmd) if err != nil { - return err + return fmt.Errorf("cannot restore '%s': %v", dbName, err) } if closer != nil { defer closer.Close() @@ -43,7 +43,7 @@ func restore(cmd *cobra.Command, args []string) error { db, err := store.open(dbName) if err != nil { - return err + return fmt.Errorf("cannot restore '%s': %v", dbName, err) } defer db.Close() @@ -66,15 +66,15 @@ func restore(cmd *cobra.Command, args []string) error { var entry dumpEntry if err := json.Unmarshal([]byte(line), &entry); err != nil { - return fmt.Errorf("line %d: %w", lineNo, err) + return fmt.Errorf("cannot restore '%s': line %d: %w", dbName, lineNo, err) } if entry.Key == "" { - return fmt.Errorf("line %d: missing key", lineNo) + return fmt.Errorf("cannot restore '%s': line %d: missing key", dbName, lineNo) } value, err := decodeEntryValue(entry) if err != nil { - return fmt.Errorf("line %d: %w", lineNo, err) + return fmt.Errorf("cannot restore '%s': line %d: %w", dbName, lineNo, err) } entryMeta := byte(0x0) @@ -85,13 +85,13 @@ func restore(cmd *cobra.Command, args []string) error { writeEntry := badger.NewEntry([]byte(entry.Key), value).WithMeta(entryMeta) if entry.ExpiresAt != nil { if *entry.ExpiresAt < 0 { - return fmt.Errorf("line %d: expires_at must be >= 0", lineNo) + return fmt.Errorf("cannot restore '%s': line %d: expires_at must be >= 0", dbName, lineNo) } writeEntry.ExpiresAt = uint64(*entry.ExpiresAt) } if err := wb.SetEntry(writeEntry); err != nil { - return fmt.Errorf("line %d: %w", lineNo, err) + return fmt.Errorf("cannot restore '%s': line %d: %w", dbName, lineNo, err) } restored++ } @@ -101,7 +101,7 @@ func restore(cmd *cobra.Command, args []string) error { } if err := wb.Flush(); err != nil { - return err + return fmt.Errorf("cannot restore '%s': %v", dbName, err) } fmt.Fprintf(cmd.ErrOrStderr(), "Restored %d entries into @%s\n", restored, dbName) diff --git a/cmd/shared.go b/cmd/shared.go index 0980610..560c6aa 100644 --- a/cmd/shared.go +++ b/cmd/shared.go @@ -46,9 +46,9 @@ const ( func (err errNotFound) Error() string { if len(err.suggestions) == 0 { - return "no suggestions found" + return "No such key" } - return fmt.Sprintf("did you mean %q", strings.Join(err.suggestions, ", ")) + return fmt.Sprintf("No such key. Did you mean '%s'?", strings.Join(err.suggestions, ", ")) } type Store struct{} @@ -189,7 +189,7 @@ func (s *Store) parseDB(v string, defaults bool) (string, error) { if defaults { return "default", nil } - return "", fmt.Errorf("bad db format, use DB or @DB") + return "", fmt.Errorf("cannot parse db: bad db format, use DB or @DB") } return strings.ToLower(db), nil }