1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00

NOISUE - Fix cache error when key is not in Redis (#1220)

* Fix cache error when key is not in Redis

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix tests

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix errors message

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
This commit is contained in:
Dušan Borovčanin 2020-07-14 22:54:33 +02:00 committed by GitHub
parent c1088b9315
commit be1361311d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 14 deletions

View File

@ -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):

View File

@ -14,14 +14,16 @@ import (
const chanPrefix = "channel"
// ErrRedisConnectChannel indicates error while adding connection in redis cache
var ErrRedisConnectChannel = errors.New("add connection to redis cache error")
var (
// ErrRedisConnectChannel indicates error while adding connection in redis cache
ErrRedisConnectChannel = errors.New("failed to add connection to redis cache")
// ErrRedisDisconnectChannel indicates error while removing connection from redis cache
var ErrRedisDisconnectChannel = errors.New("remove connection from redis cache error")
// ErrRedisDisconnectChannel indicates error while removing connection from redis cache
ErrRedisDisconnectChannel = errors.New("failed to remove connection from redis cache")
// ErrRedisRemoveChannel indicates error while removing channel from redis cache
var ErrRedisRemoveChannel = errors.New("remove channel from redis cache error")
// ErrRedisRemoveChannel indicates error while removing channel from redis cache
ErrRedisRemoveChannel = errors.New("failed to remove channel from redis cache")
)
var _ things.ChannelCache = (*channelCache)(nil)

View File

@ -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("failed to save thing in redis cache")
// 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("failed to get thing id from redis cache")
// 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("failed to remove thing from redis cache")
)
var _ things.ThingCache = (*thingCache)(nil)
@ -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)
}

View File

@ -10,8 +10,8 @@ import (
r "github.com/go-redis/redis"
"github.com/mainflux/mainflux/pkg/errors"
"github.com/mainflux/mainflux/things/redis"
"github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/things/redis"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -108,7 +108,7 @@ func TestThingRemove(t *testing.T) {
{
desc: "Remove non-existing thing from cache",
ID: id2,
err: r.Nil,
err: nil,
},
}