Skip to content

Commit

Permalink
feat(all): Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bartventer committed Jun 19, 2024
1 parent 93b7fbd commit 5ecf720
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
15 changes: 8 additions & 7 deletions memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ package memcache

import (
"context"
"errors"
"fmt"
"net/url"
"strings"
Expand Down Expand Up @@ -112,7 +113,7 @@ func (m *memcacheCache) init(_ context.Context, opts *Options) {

// Count implements cache.Cache.
func (m *memcacheCache) Count(_ context.Context, pattern string, modifiers ...keymod.Mod) (int64, error) {
return 0, gcerrors.NewWithScheme(Scheme, fmt.Errorf("Count not supported: %w", cache.ErrPatternMatchingNotSupported))
return 0, gcerrors.NewWithScheme(Scheme, errors.Join(cache.ErrPatternMatchingNotSupported, fmt.Errorf("Count operation not supported")))
}

// Exists implements cache.Cache.
Expand All @@ -123,7 +124,7 @@ func (m *memcacheCache) Exists(_ context.Context, key string, modifiers ...keymo
if err == memcache.ErrCacheMiss {
return false, nil
} else {
return false, gcerrors.NewWithScheme(Scheme, fmt.Errorf("error checking key %s, underlying error: %w", key, err))
return false, gcerrors.NewWithScheme(Scheme, fmt.Errorf("error checking key %s: %w", key, err))
}
}
return true, nil
Expand All @@ -135,17 +136,17 @@ func (m *memcacheCache) Del(_ context.Context, key string, modifiers ...keymod.M
err := m.client.Delete(key)
if err != nil {
if err == memcache.ErrCacheMiss {
return gcerrors.NewWithScheme(Scheme, fmt.Errorf("%s: %w, underlying error: %w", key, cache.ErrKeyNotFound, err))
return gcerrors.NewWithScheme(Scheme, errors.Join(cache.ErrKeyNotFound, fmt.Errorf("key %s not found: %w", key, err)))
} else {
return gcerrors.NewWithScheme(Scheme, fmt.Errorf("error deleting key %s, underlying error: %w", key, err))
return gcerrors.NewWithScheme(Scheme, fmt.Errorf("error deleting key %s: %w", key, err))
}
}
return nil
}

// DelKeys implements cache.Cache.
func (m *memcacheCache) DelKeys(_ context.Context, pattern string, modifiers ...keymod.Mod) error {
return gcerrors.NewWithScheme(Scheme, fmt.Errorf("DelKeys not supported: %w", cache.ErrPatternMatchingNotSupported))
return gcerrors.NewWithScheme(Scheme, errors.Join(cache.ErrPatternMatchingNotSupported, fmt.Errorf("DelKeys operation not supported")))
}

// Clear implements cache.Cache.
Expand All @@ -159,9 +160,9 @@ func (m *memcacheCache) Get(_ context.Context, key string, modifiers ...keymod.M
item, err := m.client.Get(key)
if err != nil {
if err == memcache.ErrCacheMiss {
return nil, gcerrors.NewWithScheme(Scheme, fmt.Errorf("%s: %w, underlying error: %w", key, cache.ErrKeyNotFound, err))
return nil, gcerrors.NewWithScheme(Scheme, errors.Join(cache.ErrKeyNotFound, fmt.Errorf("key %s not found: %w", key, err)))
} else {
return nil, gcerrors.NewWithScheme(Scheme, fmt.Errorf("error getting key %s, underlying error: %w", key, err))
return nil, gcerrors.NewWithScheme(Scheme, fmt.Errorf("error getting key %s: %w", key, err))
}
}
return item.Value, nil
Expand Down
9 changes: 5 additions & 4 deletions ramcache/ramcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import (
"context"
"encoding"
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
Expand Down Expand Up @@ -161,7 +162,7 @@ func (r *ramcache) removeExpiredItems() {

// Count implements cache.Cache.
func (r *ramcache) Count(ctx context.Context, pattern string, modifiers ...keymod.Mod) (int64, error) {
return 0, gcerrors.NewWithScheme(Scheme, fmt.Errorf("Count not supported: %w", cache.ErrPatternMatchingNotSupported))
return 0, gcerrors.NewWithScheme(Scheme, errors.Join(cache.ErrPatternMatchingNotSupported, fmt.Errorf("Count operation not supported")))
}

// Exists implements cache.Cache.
Expand All @@ -180,15 +181,15 @@ func (r *ramcache) Del(ctx context.Context, key string, modifiers ...keymod.Mod)
key = keymod.Modify(key, modifiers...)
_, exists := r.store.Get(key)
if !exists {
return gcerrors.NewWithScheme(Scheme, fmt.Errorf("%s: %w", key, cache.ErrKeyNotFound))
return gcerrors.NewWithScheme(Scheme, errors.Join(cache.ErrKeyNotFound, fmt.Errorf("key %s not found", key)))
}
r.store.Delete(key)
return nil
}

// DelKeys implements cache.Cache.
func (r *ramcache) DelKeys(ctx context.Context, pattern string, modifiers ...keymod.Mod) error {
return gcerrors.NewWithScheme(Scheme, fmt.Errorf("DelKeys not supported: %w", cache.ErrPatternMatchingNotSupported))
return gcerrors.NewWithScheme(Scheme, errors.Join(cache.ErrPatternMatchingNotSupported, fmt.Errorf("pattern %s not supported", pattern)))
}

// Clear implements cache.Cache.
Expand All @@ -203,7 +204,7 @@ func (r *ramcache) Get(ctx context.Context, key string, modifiers ...keymod.Mod)
item, exists := r.store.Get(key)
if !exists || item.IsExpired() {
r.store.Delete(key)
return nil, gcerrors.NewWithScheme(Scheme, cache.ErrKeyNotFound)
return nil, gcerrors.NewWithScheme(Scheme, errors.Join(cache.ErrKeyNotFound, fmt.Errorf("key %s not found", key)))
}
return item.Value, nil
}
Expand Down
5 changes: 3 additions & 2 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ package redis

import (
"context"
"errors"
"fmt"
"net/url"
"sync"
Expand Down Expand Up @@ -149,7 +150,7 @@ func (r *redisCache) Del(ctx context.Context, key string, modifiers ...keymod.Mo
return gcerrors.NewWithScheme(Scheme, fmt.Errorf("error deleting key %s: %w", key, err))
}
if delCount == 0 {
return gcerrors.NewWithScheme(Scheme, fmt.Errorf("%s: %w", key, cache.ErrKeyNotFound))
return gcerrors.NewWithScheme(Scheme, errors.Join(cache.ErrKeyNotFound, fmt.Errorf("key %s not found: %w", key, err)))
}
return nil
}
Expand Down Expand Up @@ -185,7 +186,7 @@ func (r *redisCache) Get(ctx context.Context, key string, modifiers ...keymod.Mo
val, err := r.client.Get(ctx, key).Bytes()
if err != nil {
if err == redis.Nil {
return nil, gcerrors.NewWithScheme(Scheme, fmt.Errorf("%s: %w, underlying error: %w", key, cache.ErrKeyNotFound, err))
return nil, gcerrors.NewWithScheme(Scheme, errors.Join(cache.ErrKeyNotFound, fmt.Errorf("key %s not found: %w", key, err)))
} else {
return nil, gcerrors.NewWithScheme(Scheme, fmt.Errorf("error getting key %s: %w", key, err))
}
Expand Down
5 changes: 3 additions & 2 deletions rediscluster/rediscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ package rediscluster

import (
"context"
"errors"
"fmt"
"net/url"
"sync"
Expand Down Expand Up @@ -161,7 +162,7 @@ func (r *redisClusterCache) Del(ctx context.Context, key string, modifiers ...ke
return gcerrors.NewWithScheme(Scheme, fmt.Errorf("error deleting key %s: %w", key, err))
}
if delCount == 0 {
return gcerrors.NewWithScheme(Scheme, fmt.Errorf("%s: %w", key, cache.ErrKeyNotFound))
return gcerrors.NewWithScheme(Scheme, errors.Join(cache.ErrKeyNotFound, fmt.Errorf("key %s not found", key)))
}
return nil
}
Expand Down Expand Up @@ -201,7 +202,7 @@ func (r *redisClusterCache) Get(ctx context.Context, key string, modifiers ...ke
val, err := r.client.Get(ctx, key).Bytes()
if err != nil {
if err == redis.Nil {
return nil, gcerrors.NewWithScheme(Scheme, fmt.Errorf("%s: %w, underlying error: %w", key, cache.ErrKeyNotFound, err))
return nil, gcerrors.NewWithScheme(Scheme, errors.Join(cache.ErrKeyNotFound, fmt.Errorf("key %s not found: %w", key, err)))
} else {
return nil, gcerrors.NewWithScheme(Scheme, fmt.Errorf("error getting key %s: %w", key, err))
}
Expand Down

0 comments on commit 5ecf720

Please sign in to comment.