From f8e851a6ed75b3cf76516e9d864b6178ff12807a Mon Sep 17 00:00:00 2001 From: Svarog Date: Mon, 4 May 2020 16:33:23 +0300 Subject: [PATCH] [NOD-968] Wrap all ldb errors with pkg/errors (#712) --- database/ffldb/ldb/cursor.go | 16 ++++++---------- database/ffldb/ldb/transaction.go | 5 +++-- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/database/ffldb/ldb/cursor.go b/database/ffldb/ldb/cursor.go index a9d1eb3d2..ebc020bba 100644 --- a/database/ffldb/ldb/cursor.go +++ b/database/ffldb/ldb/cursor.go @@ -2,6 +2,7 @@ package ldb import ( "bytes" + "github.com/kaspanet/kaspad/database" "github.com/pkg/errors" "github.com/syndtr/goleveldb/leveldb/iterator" @@ -52,20 +53,15 @@ func (c *LevelDBCursor) Seek(key *database.Key) error { return errors.New("cannot seek a closed cursor") } - notFoundErr := errors.Wrapf(database.ErrNotFound, "key %s not "+ - "found", key) found := c.ldbIterator.Seek(key.Bytes()) if !found { - return notFoundErr + return errors.Wrapf(database.ErrNotFound, "key %s not found", key) } // Use c.ldbIterator.Key because c.Key removes the prefix from the key currentKey := c.ldbIterator.Key() - if currentKey == nil { - return notFoundErr - } - if !bytes.Equal(currentKey, key.Bytes()) { - return notFoundErr + if currentKey == nil || !bytes.Equal(currentKey, key.Bytes()) { + return errors.Wrapf(database.ErrNotFound, "key %s not found", key) } return nil @@ -82,7 +78,7 @@ func (c *LevelDBCursor) Key() (*database.Key, error) { fullKeyPath := c.ldbIterator.Key() if fullKeyPath == nil { return nil, errors.Wrapf(database.ErrNotFound, "cannot get the "+ - "key of a done cursor") + "key of an exhausted cursor") } suffix := bytes.TrimPrefix(fullKeyPath, c.bucket.Path()) return c.bucket.Key(suffix), nil @@ -98,7 +94,7 @@ func (c *LevelDBCursor) Value() ([]byte, error) { value := c.ldbIterator.Value() if value == nil { return nil, errors.Wrapf(database.ErrNotFound, "cannot get the "+ - "value of a done cursor") + "value of an exhausted cursor") } return value, nil } diff --git a/database/ffldb/ldb/transaction.go b/database/ffldb/ldb/transaction.go index 41c3866da..4c3e974b0 100644 --- a/database/ffldb/ldb/transaction.go +++ b/database/ffldb/ldb/transaction.go @@ -53,7 +53,7 @@ func (tx *LevelDBTransaction) Commit() error { tx.isClosed = true tx.snapshot.Release() - return tx.db.ldb.Write(tx.batch, nil) + return errors.WithStack(tx.db.ldb.Write(tx.batch, nil)) } // Rollback rolls back whatever changes were made to the @@ -115,7 +115,8 @@ func (tx *LevelDBTransaction) Has(key *database.Key) (bool, error) { return false, errors.New("cannot has from a closed transaction") } - return tx.snapshot.Has(key.Bytes(), nil) + res, err := tx.snapshot.Has(key.Bytes(), nil) + return res, errors.WithStack(err) } // Delete deletes the value for the given key. Will not