Skip to content

Commit d3d8002

Browse files
committed
feat: add HasErrorPrefix
1 parent f9e60f2 commit d3d8002

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

error.go

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ import (
1313
// ErrClosed performs any operation on the closed client will return this error.
1414
var ErrClosed = pool.ErrClosed
1515

16+
// HasErrorPrefix checks if the err is a Redis error and the message contains a prefix.
17+
func HasErrorPrefix(err error, prefix string) bool {
18+
err, ok := err.(Error)
19+
if !ok {
20+
return false
21+
}
22+
msg := err.Error()
23+
msg = strings.TrimPrefix(msg, "ERR ") // KVRocks adds such prefix
24+
return strings.HasPrefix(msg, prefix)
25+
}
26+
1627
type Error interface {
1728
error
1829

script.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"crypto/sha1"
66
"encoding/hex"
77
"io"
8-
"strings"
98
)
109

1110
type Scripter interface {
@@ -68,7 +67,7 @@ func (s *Script) EvalShaRO(ctx context.Context, c Scripter, keys []string, args
6867
// it is retried using EVAL.
6968
func (s *Script) Run(ctx context.Context, c Scripter, keys []string, args ...interface{}) *Cmd {
7069
r := s.EvalSha(ctx, c, keys, args...)
71-
if err := r.Err(); err != nil && strings.HasPrefix(err.Error(), "NOSCRIPT ") {
70+
if HasErrorPrefix(r.Err(), "NOSCRIPT") {
7271
return s.Eval(ctx, c, keys, args...)
7372
}
7473
return r
@@ -78,7 +77,7 @@ func (s *Script) Run(ctx context.Context, c Scripter, keys []string, args ...int
7877
// it is retried using EVAL_RO.
7978
func (s *Script) RunRO(ctx context.Context, c Scripter, keys []string, args ...interface{}) *Cmd {
8079
r := s.EvalShaRO(ctx, c, keys, args...)
81-
if err := r.Err(); err != nil && strings.HasPrefix(err.Error(), "NOSCRIPT ") {
80+
if HasErrorPrefix(r.Err(), "NOSCRIPT") {
8281
return s.EvalRO(ctx, c, keys, args...)
8382
}
8483
return r

0 commit comments

Comments
 (0)