Skip to content

Commit

Permalink
Fix cache error when key is not in Redis
Browse files Browse the repository at this point in the history
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
  • Loading branch information
dborovcanin committed Jul 14, 2020
1 parent 2453cd7 commit f101bf3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions things/api/things/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
w.WriteHeader(http.StatusUnsupportedMediaType)
case errors.Contains(errorVal, errInvalidQueryParams):
w.WriteHeader(http.StatusBadRequest)
case errors.Contains(errorVal, things.ErrRemoveThing):
w.WriteHeader(http.StatusInternalServerError)
case errors.Contains(errorVal, things.ErrRemoveChannel):
w.WriteHeader(http.StatusInternalServerError)
case errors.Contains(errorVal, io.ErrUnexpectedEOF):
w.WriteHeader(http.StatusBadRequest)
case errors.Contains(errorVal, io.EOF):
Expand Down
18 changes: 12 additions & 6 deletions things/redis/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ const (
idPrefix = "thing"
)

// ErrRedisThingSave indicates error while saving Thing in redis cache
var ErrRedisThingSave = errors.New("saving thing in redis cache error")
var (
// ErrRedisThingSave indicates error while saving Thing in redis cache
ErrRedisThingSave = errors.New("saving thing in redis cache error")

// ErrRedisThingID indicates error while geting Thing ID from redis cache
var ErrRedisThingID = errors.New("get thing id from redis cache error")
// ErrRedisThingID indicates error while geting Thing ID from redis cache
ErrRedisThingID = errors.New("get thing id from redis cache error")

// ErrRedisThingRemove indicates error while removing Thing from redis cache
var ErrRedisThingRemove = errors.New("remove thing from redis cache error")
// ErrRedisThingRemove indicates error while removing Thing from redis cache
ErrRedisThingRemove = errors.New("remove thing from redis cache error")
)

var _ things.ThingCache = (*thingCache)(nil)

Expand Down Expand Up @@ -65,6 +67,10 @@ func (tc *thingCache) ID(_ context.Context, thingKey string) (string, error) {
func (tc *thingCache) Remove(_ context.Context, thingID string) error {
tid := fmt.Sprintf("%s:%s", idPrefix, thingID)
key, err := tc.client.Get(tid).Result()
// Redis returns Nil Reply when key does not exist.
if err == redis.Nil {
return nil
}
if err != nil {
return errors.Wrap(ErrRedisThingRemove, err)
}
Expand Down

0 comments on commit f101bf3

Please sign in to comment.