diff --git a/kvstore/badger/errors.go b/kvstore/badger/errors.go new file mode 100644 index 0000000..6759b30 --- /dev/null +++ b/kvstore/badger/errors.go @@ -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") +) diff --git a/kvstore/badger/kvstore_test.go b/kvstore/badger/kvstore_test.go index 7bbbcfd..bac9d3d 100644 --- a/kvstore/badger/kvstore_test.go +++ b/kvstore/badger/kvstore_test.go @@ -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" @@ -49,7 +46,7 @@ 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", @@ -57,7 +54,7 @@ func TestBadger_KVStore_BasicOperations(t *testing.T) { 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", @@ -73,7 +70,7 @@ 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", @@ -81,7 +78,7 @@ func TestBadger_KVStore_BasicOperations(t *testing.T) { key: nil, value: nil, fail: true, - expectedErr: v4.ErrEmptyKey, + expectedErr: badger.ErrBadgerUnableToGetValue, }, { desc: "Successfully deletes a value in the store", @@ -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, }, } @@ -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) @@ -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) @@ -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) } } }) @@ -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()