[NOD-968] Wrap all ldb errors with pkg/errors (#712)

This commit is contained in:
Svarog 2020-05-04 16:33:23 +03:00 committed by GitHub
parent e70a615135
commit f8e851a6ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package ldb
import ( import (
"bytes" "bytes"
"github.com/kaspanet/kaspad/database" "github.com/kaspanet/kaspad/database"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/syndtr/goleveldb/leveldb/iterator" "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") return errors.New("cannot seek a closed cursor")
} }
notFoundErr := errors.Wrapf(database.ErrNotFound, "key %s not "+
"found", key)
found := c.ldbIterator.Seek(key.Bytes()) found := c.ldbIterator.Seek(key.Bytes())
if !found { 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 // Use c.ldbIterator.Key because c.Key removes the prefix from the key
currentKey := c.ldbIterator.Key() currentKey := c.ldbIterator.Key()
if currentKey == nil { if currentKey == nil || !bytes.Equal(currentKey, key.Bytes()) {
return notFoundErr return errors.Wrapf(database.ErrNotFound, "key %s not found", key)
}
if !bytes.Equal(currentKey, key.Bytes()) {
return notFoundErr
} }
return nil return nil
@ -82,7 +78,7 @@ func (c *LevelDBCursor) Key() (*database.Key, error) {
fullKeyPath := c.ldbIterator.Key() fullKeyPath := c.ldbIterator.Key()
if fullKeyPath == nil { if fullKeyPath == nil {
return nil, errors.Wrapf(database.ErrNotFound, "cannot get the "+ 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()) suffix := bytes.TrimPrefix(fullKeyPath, c.bucket.Path())
return c.bucket.Key(suffix), nil return c.bucket.Key(suffix), nil
@ -98,7 +94,7 @@ func (c *LevelDBCursor) Value() ([]byte, error) {
value := c.ldbIterator.Value() value := c.ldbIterator.Value()
if value == nil { if value == nil {
return nil, errors.Wrapf(database.ErrNotFound, "cannot get the "+ return nil, errors.Wrapf(database.ErrNotFound, "cannot get the "+
"value of a done cursor") "value of an exhausted cursor")
} }
return value, nil return value, nil
} }

View File

@ -53,7 +53,7 @@ func (tx *LevelDBTransaction) Commit() error {
tx.isClosed = true tx.isClosed = true
tx.snapshot.Release() 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 // 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 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 // Delete deletes the value for the given key. Will not