Skip to content

Commit

Permalink
Merge pull request #140 from aduffeck/return-error-on-not-found
Browse files Browse the repository at this point in the history
Return store.ErrNotFound to be consistent with other stores
  • Loading branch information
kobergj authored Jun 3, 2024
2 parents 94a49ba + 3f3d73b commit 11eebae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion v4/store/nats-js-kv/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func (n *natsStore) mustGetBucket(kv *nats.KeyValueConfig) (nats.KeyValue, error
func (n *natsStore) getRecord(bucket nats.KeyValue, key string) (*store.Record, bool, error) {
obj, err := bucket.Get(key)
if errors.Is(err, nats.ErrKeyNotFound) {
return nil, false, nil
return nil, false, store.ErrNotFound
} else if err != nil {
return nil, false, errors.Wrap(err, "Failed to get object from bucket")
}
Expand Down
18 changes: 8 additions & 10 deletions v4/store/nats-js-kv/nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import (
func TestNats(t *testing.T) {
// Setup without calling Init on purpose
var err error
var cancel func()
var ctx context.Context
for i := 0; i < 5; i++ {
ctx, cancel = context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr := startNatsServer(ctx, t)
s := NewStore(store.Nodes(addr), EncodeKeys())

Expand All @@ -33,8 +32,8 @@ func TestNats(t *testing.T) {

// Test reading non-existing key
r, err := s.Read("this-is-a-random-key")
if err != nil {
t.Fatal(err)
if !errors.Is(err, store.ErrNotFound) {
t.Errorf("Expected %# v, got %# v", store.ErrNotFound, err)
}
if len(r) > 0 {
t.Fatal("Lenth should be 0")
Expand All @@ -46,7 +45,6 @@ func TestNats(t *testing.T) {
cancel()
return
}
cancel()
t.Fatal(err)
}

Expand Down Expand Up @@ -111,8 +109,8 @@ func TestTTL(t *testing.T) {

for _, r := range table {
res, err := s.Read(r.Record.Key, store.ReadFrom(r.Database+id, r.Table))
if err != nil {
t.Fatal(err)
if !errors.Is(err, store.ErrNotFound) {
t.Errorf("Expected %# v, got %# v", store.ErrNotFound, err)
}
if len(res) > 0 {
t.Fatal("Fetched record while it should have expired")
Expand Down Expand Up @@ -170,8 +168,8 @@ func TestDelete(t *testing.T) {
time.Sleep(time.Second)

res, err := s.Read(r.Record.Key, store.ReadFrom(r.Database, r.Table))
if err != nil {
t.Fatal(err)
if !errors.Is(err, store.ErrNotFound) {
t.Errorf("Expected %# v, got %# v", store.ErrNotFound, err)
}
if len(res) > 0 {
t.Fatalf("Failed to delete %s:%s from %s %s (len: %d)", r.Record.Key, r.Record.Value, r.Database, r.Table, len(res))
Expand Down

0 comments on commit 11eebae

Please sign in to comment.