Skip to content

Commit

Permalink
Added error key not found if Get not returned value
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Cataldo committed Mar 12, 2024
1 parent 3895b06 commit 3245dcc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!--suppress ALL -->
<img align="right" src="gopher-redis.png" alt="">

[![Project status](https://img.shields.io/badge/version-v1.1.3-vividgreen.svg)](https://github.com/GabrielHCataldo/go-redis-template/releases/tag/v1.1.3)
[![Project status](https://img.shields.io/badge/version-v1.1.4-vividgreen.svg)](https://github.com/GabrielHCataldo/go-redis-template/releases/tag/v1.1.4)
[![Go Report Card](https://goreportcard.com/badge/github.com/GabrielHCataldo/go-redis-template)](https://goreportcard.com/report/github.com/GabrielHCataldo/go-redis-template)
[![Coverage Status](https://coveralls.io/repos/GabrielHCataldo/go-redis-template/badge.svg?branch=main&service=github)](https://coveralls.io/github/GabrielHCataldo/go-mongo?branch=main)
[![Open Source Helpers](https://www.codetriage.com/gabrielhcataldo/go-redis-template/badges/users.svg)](https://www.codetriage.com/gabrielhcataldo/go-mongo)
Expand Down
36 changes: 2 additions & 34 deletions redis/errors.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,17 @@
package redis

import (
"github.com/GabrielHCataldo/go-errors/errors"
"errors"
)

var MsgErrConvertKey = "redis: error convert key to string"
var MsgErrConvertKeyIndex = "redis: error convert key to string on index:"
var MsgErrConvertNewKey = "redis: error convert new key to string"
var MsgErrConvertValue = "redis: error convert value"
var MsgErrDestIsNotPointer = "redis: dest is not pointer"
var MsgErrKeyNotFound = "redis: key not exists"
var MsgErrKeyNotFound = "redis: key not found"

var ErrConvertKey = errors.New(MsgErrConvertKey)
var ErrConvertKeyIndex = errors.New(MsgErrConvertKeyIndex)
var ErrConvertNewKey = errors.New(MsgErrConvertNewKey)
var ErrConvertValue = errors.New(MsgErrConvertValue)
var ErrDestIsNotPointer = errors.New(MsgErrDestIsNotPointer)
var ErrKeyNotFound = errors.New(MsgErrKeyNotFound)

func errConvertKey(skip int) error {
ErrConvertKey = errors.NewSkipCaller(skip+1, MsgErrConvertKey)
return ErrConvertKey
}

func errConvertKeyIndex(skip, index int) error {
ErrConvertKeyIndex = errors.NewSkipCaller(skip+1, MsgErrConvertKeyIndex, index)
return ErrConvertKeyIndex
}

func errConvertNewKey(skip int) error {
ErrConvertNewKey = errors.NewSkipCaller(skip+1, MsgErrConvertNewKey)
return ErrConvertNewKey
}

func errConvertValue(skip int) error {
ErrConvertValue = errors.NewSkipCaller(skip+1, MsgErrConvertValue)
return ErrConvertValue
}

func errDestIsNotPointer(skip int) error {
ErrDestIsNotPointer = errors.NewSkipCaller(skip+1, MsgErrDestIsNotPointer)
return ErrDestIsNotPointer
}

func errKeyNotFound(skip int) error {
ErrKeyNotFound = errors.NewSkipCaller(skip+1, MsgErrKeyNotFound)
return ErrKeyNotFound
}
59 changes: 28 additions & 31 deletions redis/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package redis

import (
"context"
"errors"
"fmt"
"github.com/GabrielHCataldo/go-errors/errors"
"github.com/GabrielHCataldo/go-helper/helper"
"github.com/GabrielHCataldo/go-logger/logger"
"github.com/GabrielHCataldo/go-redis-template/redis/option"
Expand Down Expand Up @@ -54,7 +54,7 @@ func NewTemplate(opts option.Client) *Template {
func (t *Template) Set(ctx context.Context, key, value any, opts ...*option.Set) error {
result, err := t.set(ctx, key, value, false, opts...)
if helper.IsNil(err) {
err = errors.NewSkipCaller(2, result.Err())
err = result.Err()
}
return err
}
Expand All @@ -71,7 +71,7 @@ func (t *Template) MSet(ctx context.Context, values ...MSetInput) []MSetOutput {
for _, v := range values {
result, err := t.set(ctx, v.Key, v.Value, false, v.Opt)
if helper.IsNil(err) {
err = errors.NewSkipCaller(2, result.Err())
err = result.Err()
}
output = append(output, MSetOutput{
Key: v.Key,
Expand All @@ -97,10 +97,9 @@ func (t *Template) SetGet(ctx context.Context, key, value, dest any, opts ...*op
if helper.IsNotNil(err) {
return err
} else if helper.IsNotNil(result.Err()) {
return errors.NewSkipCaller(2, result.Err())
return result.Err()
}
err = helper.ConvertToDest(result.Val(), dest)
return errors.NewSkipCaller(2, err)
return helper.ConvertToDest(result.Val(), dest)
}

// Rename redis key.
Expand All @@ -112,62 +111,60 @@ func (t *Template) SetGet(ctx context.Context, key, value, dest any, opts ...*op
func (t *Template) Rename(ctx context.Context, key, newKey any) error {
sKey, err := helper.ConvertToString(key)
if helper.IsNotNil(err) {
return errConvertKey(2)
return ErrConvertKey
}
sNewKey, err := helper.ConvertToString(newKey)
if helper.IsNotNil(err) {
return errConvertNewKey(2)
return ErrConvertNewKey
}
err = t.client.Rename(ctx, sKey, sNewKey).Err()
return errors.NewSkipCaller(2, err)
return t.client.Rename(ctx, sKey, sNewKey).Err()
}

// Get redis `GET key` command.
//
// The key parameter can be of any type, but cannot be null, in case an error occurs when converting, the error
// returned is ErrConvertKey.
// returned is ErrConvertKey. If no registered key is found, the error ErrKeyNotFound is returned.
//
// The dest parameter must be a pointer, not null, if we do not find any value, dest will not have any modification.
// The dest parameter must be a pointer.
//
// If the return is null, the operation was performed successfully, otherwise an error occurred in the operation.
func (t *Template) Get(ctx context.Context, key, dest any) error {
if !helper.IsPointer(dest) {
return errDestIsNotPointer(2)
return ErrDestIsNotPointer
}
sKey, err := helper.ConvertToString(key)
if helper.IsNotNil(err) {
return errConvertKey(2)
return ErrConvertKey
}
result, err := t.client.Get(ctx, sKey).Result()
if errors.Is(err, redis.Nil) {
return nil
return ErrKeyNotFound
}
err = helper.ConvertToDest(result, dest)
return errors.NewSkipCaller(2, err)
return helper.ConvertToDest(result, dest)
}

// GetDel get and delete value by key.
//
// The key parameter can be of any type, but cannot be null, if an error occurs during the conversion, the error
// returned is ErrConvertKey. If no registered key is found, the error ErrKeyNotFound is returned.
//
// The dest parameter must be a pointer, not null, if we do not find any value, dest will not have any modification.
// The dest parameter must be a pointer.
//
// If the return is null, the operation was performed successfully, otherwise an error occurred in the operation.
func (t *Template) GetDel(ctx context.Context, key, dest any) error {
sKey, err := helper.ConvertToString(key)
if helper.IsNotNil(err) {
return errConvertKey(2)
return ErrConvertKey
}
result := t.client.GetDel(ctx, sKey)
if helper.IsNotNil(result.Err()) {
err = errors.NewSkipCaller(2, result.Err())
err = result.Err()
if errors.Is(result.Err(), redis.Nil) {
err = errKeyNotFound(2)
err = ErrKeyNotFound
}
return err
}
return errors.NewSkipCaller(2, helper.ConvertToDest(result.Val(), dest))
return helper.ConvertToDest(result.Val(), dest)
}

// Exists redis values by key.
Expand All @@ -180,10 +177,10 @@ func (t *Template) GetDel(ctx context.Context, key, dest any) error {
func (t *Template) Exists(ctx context.Context, key any) (bool, error) {
sKey, err := helper.ConvertToString(key)
if helper.IsNotNil(err) {
return false, errConvertKey(2)
return false, ErrConvertKey
}
result := t.client.Exists(ctx, sKey)
return helper.IsGreaterThan(result.Val(), 0), errors.NewSkipCaller(2, result.Err())
return helper.IsGreaterThan(result.Val(), 0), result.Err()
}

// Keys return list of keys by pattern.
Expand All @@ -204,19 +201,19 @@ func (t *Template) Scan(ctx context.Context, cursor uint64, match string, count
// Del delete redis keys.
//
// The keys parameter can be of any type, but cannot be empty, if an error occurs during the conversion, the error
// returned is ErrConvertKeyIndex.
// returned is ErrConvertKey.
//
// If the return is null, the operation was performed successfully, otherwise an error occurred in the operation.
func (t *Template) Del(ctx context.Context, keys ...any) error {
var sKeys []string
for i, key := range keys {
for _, key := range keys {
sKey, err := helper.ConvertToString(key)
if helper.IsNotNil(err) {
return errConvertKeyIndex(2, i)
return ErrConvertKey
}
sKeys = append(sKeys, sKey)
}
return errors.NewSkipCaller(2, t.client.Del(ctx, sKeys...).Err())
return t.client.Del(ctx, sKeys...).Err()
}

// SprintKey format values as prefix in string for a future redis key, ex: "test", "test2" -> "test:test2"
Expand All @@ -237,7 +234,7 @@ func (t *Template) SprintKey(vs ...any) string {

// Disconnect close connection to redis
func (t *Template) Disconnect() error {
return errors.NewSkipCaller(2, t.client.Close())
return t.client.Close()
}

// SimpleDisconnect close connection to redis without error
Expand All @@ -260,11 +257,11 @@ func (t *Template) set(
opt := option.GetOptionSetByParams(opts)
sKey, err := helper.ConvertToString(key)
if helper.IsNotNil(err) {
return nil, errConvertKey(3)
return nil, ErrConvertKey
}
sValue, err := helper.ConvertToString(value)
if helper.IsNotNil(err) {
return nil, errConvertValue(3)
return nil, ErrConvertValue
}
return t.client.SetArgs(ctx, sKey, sValue, redis.SetArgs{
Mode: opt.Mode.String(),
Expand Down
1 change: 0 additions & 1 deletion redis/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ func TestTemplateDel(t *testing.T) {
return
}
logger.Info("result:", err)
logger.Info("result err convert key equal:", errors.Is(err, ErrConvertKeyIndex))
})
}
}
Expand Down

0 comments on commit 3245dcc

Please sign in to comment.