Skip to content

Commit

Permalink
Fix redis nil error
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed Mar 29, 2020
1 parent 0870ae0 commit 3786d00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 11 additions & 3 deletions stores/redisstore/store.go
Expand Up @@ -65,16 +65,16 @@ func (s *Store) Get(id string, clear bool) (string, error) {
}
_, err := tx.Exec()
if err != nil {
return "", err
return "", s.handleError(err)
}
val, err := get.Result()
isNil := false
if err != nil {
isNil = err == redis.Nil
if isNil {
if err == redis.Nil {
return "", captchas.ErrCaptchaIncorrect
}
return "", err
return "", s.handleError(err)
}

if clear && !isNil {
Expand All @@ -95,3 +95,11 @@ func (s *Store) Set(id string, value string) error {
}
return nil
}

func (s *Store) handleError(err error) error {
if err == redis.Nil {
return captchas.ErrCaptchaIncorrect
}

return err
}
4 changes: 4 additions & 0 deletions stores/redisstore/store_test.go
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/clevergo/captchas"
"github.com/go-redis/redis/v7"
)

Expand Down Expand Up @@ -79,6 +80,9 @@ func TestStoreGet(t *testing.T) {
if err == nil {
t.Error("expected a non-nil error, got nil")
}
if err != captchas.ErrCaptchaIncorrect {
t.Errorf("expected error %v, got %v", captchas.ErrCaptchaIncorrect, err)
}
}

func TestStoreSet(t *testing.T) {
Expand Down

0 comments on commit 3786d00

Please sign in to comment.