Skip to content

Commit

Permalink
feat: add HasErrorPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Nov 17, 2022
1 parent f9e60f2 commit d3d8002
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 11 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ import (
// ErrClosed performs any operation on the closed client will return this error.
var ErrClosed = pool.ErrClosed

// HasErrorPrefix checks if the err is a Redis error and the message contains a prefix.
func HasErrorPrefix(err error, prefix string) bool {
err, ok := err.(Error)
if !ok {
return false
}
msg := err.Error()
msg = strings.TrimPrefix(msg, "ERR ") // KVRocks adds such prefix
return strings.HasPrefix(msg, prefix)
}

type Error interface {
error

Expand Down
5 changes: 2 additions & 3 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/sha1"
"encoding/hex"
"io"
"strings"
)

type Scripter interface {
Expand Down Expand Up @@ -68,7 +67,7 @@ func (s *Script) EvalShaRO(ctx context.Context, c Scripter, keys []string, args
// it is retried using EVAL.
func (s *Script) Run(ctx context.Context, c Scripter, keys []string, args ...interface{}) *Cmd {
r := s.EvalSha(ctx, c, keys, args...)
if err := r.Err(); err != nil && strings.HasPrefix(err.Error(), "NOSCRIPT ") {
if HasErrorPrefix(r.Err(), "NOSCRIPT") {
return s.Eval(ctx, c, keys, args...)
}
return r
Expand All @@ -78,7 +77,7 @@ func (s *Script) Run(ctx context.Context, c Scripter, keys []string, args ...int
// it is retried using EVAL_RO.
func (s *Script) RunRO(ctx context.Context, c Scripter, keys []string, args ...interface{}) *Cmd {
r := s.EvalShaRO(ctx, c, keys, args...)
if err := r.Err(); err != nil && strings.HasPrefix(err.Error(), "NOSCRIPT ") {
if HasErrorPrefix(r.Err(), "NOSCRIPT") {
return s.EvalRO(ctx, c, keys, args...)
}
return r
Expand Down

0 comments on commit d3d8002

Please sign in to comment.