Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 8 additions & 6 deletions things/redis/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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("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)

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
4 changes: 2 additions & 2 deletions things/redis/things_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestThingRemove(t *testing.T) {
{
desc: "Remove non-existing thing from cache",
ID: id2,
err: r.Nil,
err: nil,
},
}

Expand Down