Skip to content

Commit

Permalink
chore: use custom errors and wrap badgerv4 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
h5law committed Dec 23, 2023
1 parent 7e57c58 commit 606afae
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
38 changes: 38 additions & 0 deletions kvstore/badger/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package badger

import (
"errors"
)

var (
// ErrBadgerOpeningStore is returned when the badger store cannot be opened
// or an error occurs while opening/creating the KVStore
ErrBadgerOpeningStore = errors.New("error opening the store")
// ErrBadgerUnableToSetValue is returned when the badger store fails to
// set a value
ErrBadgerUnableToSetValue = errors.New("unable to set value")
// ErrBadgerUnableToGetValue is returned when the badger store fails to
// retrieve a value
ErrBadgerUnableToGetValue = errors.New("unable to get value")
// ErrBadgerUnableToDeleteValue is returned when the badger store fails to
// delete a value
ErrBadgerUnableToDeleteValue = errors.New("unable to delete value")
// ErrBadgerIteratingStore is returned when the badger store fails to
// iterate over the database
ErrBadgerIteratingStore = errors.New("unable to iterate over database")
// ErrBadgerClearingStore is returned when the badger store fails to
// clear all values
ErrBadgerClearingStore = errors.New("unable to clear store")
// ErrBadgerUnableToBackup is returned when the badger store fails to
// backup the database
ErrBadgerUnableToBackup = errors.New("unable to backup database")
// ErrBadgerUnableToRestore is returned when the badger store fails to
// restore the database
ErrBadgerUnableToRestore = errors.New("unable to restore database")
// ErrBadgerClosingStore is returned when the badger store fails to
// close the database
ErrBadgerClosingStore = errors.New("unable to close database")
// ErrBadgerGettingStoreLength is returned when the badger store fails to
// get the length of the database
ErrBadgerGettingStoreLength = errors.New("unable to get database length")
)
29 changes: 13 additions & 16 deletions kvstore/badger/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package badger_test

import (
"bytes"
"encoding/hex"
"fmt"
"strings"
"testing"

v4 "github.com/dgraph-io/badger/v4"
"github.com/stretchr/testify/require"

"github.com/pokt-network/smt/badger"
Expand Down Expand Up @@ -49,15 +46,15 @@ func TestBadger_KVStore_BasicOperations(t *testing.T) {
key: nil,
value: []byte("bar"),
fail: true,
expectedErr: v4.ErrEmptyKey,
expectedErr: badger.ErrBadgerUnableToSetValue,
},
{
desc: "Fails to set a value to a key that is too large",
op: "set",
key: invalidKey[:],
value: []byte("bar"),
fail: true,
expectedErr: fmt.Errorf("Key with size 65001 exceeded 65000 limit. Key:\n%s", hex.Dump(invalidKey[:1<<10])),
expectedErr: badger.ErrBadgerUnableToSetValue,
},
{
desc: "Successfully retrieve a value from the store",
Expand All @@ -73,15 +70,15 @@ func TestBadger_KVStore_BasicOperations(t *testing.T) {
key: []byte("bar"),
value: nil,
fail: true,
expectedErr: v4.ErrKeyNotFound,
expectedErr: badger.ErrBadgerUnableToGetValue,
},
{
desc: "Fails when the key is empty",
op: "get",
key: nil,
value: nil,
fail: true,
expectedErr: v4.ErrEmptyKey,
expectedErr: badger.ErrBadgerUnableToGetValue,
},
{
desc: "Successfully deletes a value in the store",
Expand All @@ -100,20 +97,20 @@ func TestBadger_KVStore_BasicOperations(t *testing.T) {
expectedErr: nil,
},
{
desc: "Fails to set value to nil key",
desc: "Fails to delete a nil key",
op: "delete",
key: nil,
value: nil,
fail: true,
expectedErr: v4.ErrEmptyKey,
expectedErr: badger.ErrBadgerUnableToDeleteValue,
},
{
desc: "Fails to set a value to a key that is too large",
desc: "Fails to delete a value for a key that is too large",
op: "delete",
key: invalidKey[:],
value: nil,
fail: true,
expectedErr: fmt.Errorf("Key with size 65001 exceeded 65000 limit. Key:\n%s", hex.Dump(invalidKey[:1<<10])),
expectedErr: badger.ErrBadgerUnableToDeleteValue,
},
}

Expand All @@ -127,7 +124,7 @@ func TestBadger_KVStore_BasicOperations(t *testing.T) {
err := store.Set(tc.key, tc.value)
if tc.fail {
require.Error(t, err)
require.EqualError(t, tc.expectedErr, err.Error())
require.ErrorIs(t, err, tc.expectedErr)
} else {
require.NoError(t, err)
got, err := store.Get(tc.key)
Expand All @@ -138,7 +135,7 @@ func TestBadger_KVStore_BasicOperations(t *testing.T) {
got, err := store.Get(tc.key)
if tc.fail {
require.Error(t, err)
require.EqualError(t, tc.expectedErr, err.Error())
require.ErrorIs(t, err, tc.expectedErr)
} else {
require.NoError(t, err)
require.Equal(t, tc.value, got)
Expand All @@ -147,11 +144,11 @@ func TestBadger_KVStore_BasicOperations(t *testing.T) {
err := store.Delete(tc.key)
if tc.fail {
require.Error(t, err)
require.EqualError(t, tc.expectedErr, err.Error())
require.ErrorIs(t, err, tc.expectedErr)
} else {
require.NoError(t, err)
_, err := store.Get(tc.key)
require.EqualError(t, err, v4.ErrKeyNotFound.Error())
require.ErrorIs(t, err, badger.ErrBadgerUnableToGetValue)
}
}
})
Expand Down Expand Up @@ -283,7 +280,7 @@ func TestBadger_KVStore_Exists(t *testing.T) {

// Key does not exist
exists, err = store.Exists([]byte("oof"))
require.EqualError(t, err, v4.ErrKeyNotFound.Error())
require.ErrorIs(t, err, badger.ErrBadgerUnableToGetValue)
require.False(t, exists)

err = store.Stop()
Expand Down

0 comments on commit 606afae

Please sign in to comment.