handle error when deleting non existing key

This commit is contained in:
2025-03-16 23:26:20 +01:00
parent 6880401ac8
commit de0589761e
3 changed files with 15 additions and 1 deletions

View File

@@ -248,6 +248,7 @@ var deleteCmd = &cobra.Command{
if err != nil {
Logger.Error(err)
return
}
if *success {

View File

@@ -140,6 +140,9 @@ func (t *Query) Delete(np *VaultService, success *bool) error {
err := val.Delete(np.Service)
if err != nil {
*success = false
if err == storage.ErrNotFound {
return nil
}
return err
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"time"
"errors"
"github.com/ipfs/go-datastore/query"
pubsub "github.com/libp2p/go-libp2p-pubsub"
@@ -18,6 +19,8 @@ import (
. "github.com/k4lipso/pentapass/internal/log"
)
var ErrNotFound error = errors.New("Not found")
type Vault struct {
ID string
Datastore *crdt.Datastore
@@ -73,8 +76,15 @@ func (n *Vault) Put(k string, v string) error {
}
func (n *Vault) Delete(k string) error {
_, err := n.GetPassword(k)
if err != nil {
return ErrNotFound
}
key := ds.NewKey(k)
err := n.Datastore.Delete(n.ctx, key)
err = n.Datastore.Delete(n.ctx, key)
if err != nil {
printErr(err)