mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-27 13:48:49 +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:
parent
c1088b9315
commit
be1361311d
@ -398,6 +398,10 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
|
|||||||
w.WriteHeader(http.StatusUnsupportedMediaType)
|
w.WriteHeader(http.StatusUnsupportedMediaType)
|
||||||
case errors.Contains(errorVal, errInvalidQueryParams):
|
case errors.Contains(errorVal, errInvalidQueryParams):
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
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):
|
case errors.Contains(errorVal, io.ErrUnexpectedEOF):
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
case errors.Contains(errorVal, io.EOF):
|
case errors.Contains(errorVal, io.EOF):
|
||||||
|
@ -14,14 +14,16 @@ import (
|
|||||||
|
|
||||||
const chanPrefix = "channel"
|
const chanPrefix = "channel"
|
||||||
|
|
||||||
// ErrRedisConnectChannel indicates error while adding connection in redis cache
|
var (
|
||||||
var ErrRedisConnectChannel = errors.New("add connection to redis cache error")
|
// 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
|
// ErrRedisDisconnectChannel indicates error while removing connection from redis cache
|
||||||
var ErrRedisDisconnectChannel = errors.New("remove connection from redis cache error")
|
ErrRedisDisconnectChannel = errors.New("failed to remove connection from redis cache")
|
||||||
|
|
||||||
// ErrRedisRemoveChannel indicates error while removing channel from redis cache
|
// ErrRedisRemoveChannel indicates error while removing channel from redis cache
|
||||||
var ErrRedisRemoveChannel = errors.New("remove channel from redis cache error")
|
ErrRedisRemoveChannel = errors.New("failed to remove channel from redis cache")
|
||||||
|
)
|
||||||
|
|
||||||
var _ things.ChannelCache = (*channelCache)(nil)
|
var _ things.ChannelCache = (*channelCache)(nil)
|
||||||
|
|
||||||
|
@ -17,14 +17,16 @@ const (
|
|||||||
idPrefix = "thing"
|
idPrefix = "thing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrRedisThingSave indicates error while saving Thing in redis cache
|
var (
|
||||||
var ErrRedisThingSave = errors.New("saving thing in redis cache error")
|
// 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
|
// ErrRedisThingID indicates error while geting Thing ID from redis cache
|
||||||
var ErrRedisThingID = errors.New("get thing id from redis cache error")
|
ErrRedisThingID = errors.New("failed to get thing id from redis cache")
|
||||||
|
|
||||||
// ErrRedisThingRemove indicates error while removing Thing from redis cache
|
// ErrRedisThingRemove indicates error while removing Thing from redis cache
|
||||||
var ErrRedisThingRemove = errors.New("remove thing from redis cache error")
|
ErrRedisThingRemove = errors.New("failed to remove thing from redis cache")
|
||||||
|
)
|
||||||
|
|
||||||
var _ things.ThingCache = (*thingCache)(nil)
|
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 {
|
func (tc *thingCache) Remove(_ context.Context, thingID string) error {
|
||||||
tid := fmt.Sprintf("%s:%s", idPrefix, thingID)
|
tid := fmt.Sprintf("%s:%s", idPrefix, thingID)
|
||||||
key, err := tc.client.Get(tid).Result()
|
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 {
|
if err != nil {
|
||||||
return errors.Wrap(ErrRedisThingRemove, err)
|
return errors.Wrap(ErrRedisThingRemove, err)
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,8 @@ import (
|
|||||||
|
|
||||||
r "github.com/go-redis/redis"
|
r "github.com/go-redis/redis"
|
||||||
"github.com/mainflux/mainflux/pkg/errors"
|
"github.com/mainflux/mainflux/pkg/errors"
|
||||||
"github.com/mainflux/mainflux/things/redis"
|
|
||||||
"github.com/mainflux/mainflux/pkg/uuid"
|
"github.com/mainflux/mainflux/pkg/uuid"
|
||||||
|
"github.com/mainflux/mainflux/things/redis"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@ -108,7 +108,7 @@ func TestThingRemove(t *testing.T) {
|
|||||||
{
|
{
|
||||||
desc: "Remove non-existing thing from cache",
|
desc: "Remove non-existing thing from cache",
|
||||||
ID: id2,
|
ID: id2,
|
||||||
err: r.Nil,
|
err: nil,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user