Skip to content

Commit

Permalink
chore(cart): Add error logging for Exists check (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
carstendietrich authored Apr 3, 2024
1 parent 512786c commit be07d9f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 14 additions & 3 deletions cart/redis/infrastructure/redisStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
cartDomain "flamingo.me/flamingo-commerce/v3/cart/domain/cart"
"flamingo.me/flamingo-commerce/v3/cart/infrastructure"
"flamingo.me/flamingo/v3/core/healthcheck/domain/healthcheck"
"flamingo.me/flamingo/v3/framework/flamingo"

"github.com/redis/go-redis/v9"
)
Expand All @@ -29,6 +30,7 @@ type (
// time to live
ttlGuest time.Duration
ttlCustomer time.Duration
logger flamingo.Logger
}

// CartSerializer serializes carts in order to store them in redis
Expand All @@ -51,6 +53,7 @@ var (

// Inject dependencies and build redis client
func (r *RedisStorage) Inject(
logger flamingo.Logger,
serializer CartSerializer,
config *struct {
RedisKeyPrefix string `inject:"config:commerce.contrib.cart.redis.keyPrefix"`
Expand All @@ -64,6 +67,7 @@ func (r *RedisStorage) Inject(
RedisTLS bool `inject:"config:commerce.contrib.cart.redis.tls,optional"`
},
) *RedisStorage {
r.logger = logger
r.serializer = serializer

if config == nil {
Expand Down Expand Up @@ -107,8 +111,8 @@ func (r *RedisStorage) Inject(
// GetCart fetches a cart from redis and deserializes it
func (r *RedisStorage) GetCart(ctx context.Context, id string) (*cartDomain.Cart, error) {
cmd := r.client.Get(ctx, r.keyPrefix+id)
if cmd.Err() != nil {
return nil, fmt.Errorf("could not get cart: %w", cmd.Err())
if err := cmd.Err(); err != nil {
return nil, fmt.Errorf("could not get cart: %w", err)
}

b, err := cmd.Bytes()
Expand All @@ -126,7 +130,14 @@ func (r *RedisStorage) GetCart(ctx context.Context, id string) (*cartDomain.Cart

// HasCart checks if the cart id exists as a key in redis
func (r *RedisStorage) HasCart(ctx context.Context, id string) bool {
return r.client.Exists(ctx, r.keyPrefix+id).Val() > 0
cmd := r.client.Exists(ctx, r.keyPrefix+id)
if err := cmd.Err(); err != nil {
r.logger.WithContext(ctx).WithField(flamingo.LogKeyModule, "RedisStorage").Warn(fmt.Errorf("HasCart: couldn't check redis exists: %w returned value: %q", err, cmd.Val()))

return false
}

return cmd.Val() > 0
}

// StoreCart serializes a cart and stores it in redis
Expand Down
2 changes: 2 additions & 0 deletions cart/redis/infrastructure/redisStorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"flamingo.me/flamingo-commerce-contrib/cart/redis/infrastructure"
cartDomain "flamingo.me/flamingo-commerce/v3/cart/domain/cart"
"flamingo.me/flamingo/v3/framework/flamingo"
"github.com/go-test/deep"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
Expand All @@ -34,6 +35,7 @@ var (

func getRedisStorage(network, address string) *infrastructure.RedisStorage {
return new(infrastructure.RedisStorage).Inject(
flamingo.NullLogger{},
&infrastructure.GobSerializer{},
&struct {
RedisKeyPrefix string `inject:"config:commerce.contrib.cart.redis.keyPrefix"`
Expand Down

0 comments on commit be07d9f

Please sign in to comment.