Skip to content

Commit

Permalink
refactor: use errors.Is when checking Redis client errors (#23)
Browse files Browse the repository at this point in the history
When using `==`, this comparison will fail if the error is wrapped (for
example, to add some common prefix string to the error message.)

The solution is to use `errors.Is`, which will check down the chain.
  • Loading branch information
cwaldren-ld authored Sep 11, 2024
1 parent fc72569 commit 527d19f
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions redis_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ldredis

import (
"context"
"errors"
"github.com/launchdarkly/go-sdk-common/v3/ldlog"
"github.com/launchdarkly/go-server-sdk/v6/subsystems/ldstoretypes"
"github.com/redis/go-redis/v9"
Expand Down Expand Up @@ -59,7 +60,7 @@ func (store *redisDataStoreImpl) Get(
) (ldstoretypes.SerializedItemDescriptor, error) {
data, err := store.client.HGet(defaultContext(), store.keyForKind(kind), key).Result()
if err != nil {
if err == redis.Nil {
if errors.Is(err, redis.Nil) {
store.loggers.Debugf("Key: %s not found in \"%s\"", key, kind.GetName())
return ldstoretypes.SerializedItemDescriptor{}.NotFound(), nil
}
Expand All @@ -73,7 +74,7 @@ func (store *redisDataStoreImpl) GetAll(
kind ldstoretypes.DataKind,
) ([]ldstoretypes.KeyedSerializedItemDescriptor, error) {
values, err := store.client.HGetAll(defaultContext(), store.keyForKind(kind)).Result()
if err != nil && err != redis.Nil {
if err != nil && !errors.Is(err, redis.Nil) {
return nil, err
}

Expand Down Expand Up @@ -132,7 +133,7 @@ func (store *redisDataStoreImpl) Upsert(
if err == nil {
result, err := pipe.Exec(defaultContext())
// if exec returned nothing, it means the watch was triggered and we should retry
if (err == nil && len(result) == 0) || err == redis.TxFailedErr {
if (err == nil && len(result) == 0) || errors.Is(err, redis.TxFailedErr) {
store.loggers.Debug("Concurrent modification detected, retrying")
return nil
}
Expand Down

0 comments on commit 527d19f

Please sign in to comment.