forked from mirror/redis
feat: add HasErrorPrefix
This commit is contained in:
parent
f9e60f214b
commit
d3d8002e89
11
error.go
11
error.go
|
@ -13,6 +13,17 @@ import (
|
||||||
// ErrClosed performs any operation on the closed client will return this error.
|
// ErrClosed performs any operation on the closed client will return this error.
|
||||||
var ErrClosed = pool.ErrClosed
|
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 {
|
type Error interface {
|
||||||
error
|
error
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Scripter interface {
|
type Scripter interface {
|
||||||
|
@ -68,7 +67,7 @@ func (s *Script) EvalShaRO(ctx context.Context, c Scripter, keys []string, args
|
||||||
// it is retried using EVAL.
|
// it is retried using EVAL.
|
||||||
func (s *Script) Run(ctx context.Context, c Scripter, keys []string, args ...interface{}) *Cmd {
|
func (s *Script) Run(ctx context.Context, c Scripter, keys []string, args ...interface{}) *Cmd {
|
||||||
r := s.EvalSha(ctx, c, keys, args...)
|
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 s.Eval(ctx, c, keys, args...)
|
||||||
}
|
}
|
||||||
return r
|
return r
|
||||||
|
@ -78,7 +77,7 @@ func (s *Script) Run(ctx context.Context, c Scripter, keys []string, args ...int
|
||||||
// it is retried using EVAL_RO.
|
// it is retried using EVAL_RO.
|
||||||
func (s *Script) RunRO(ctx context.Context, c Scripter, keys []string, args ...interface{}) *Cmd {
|
func (s *Script) RunRO(ctx context.Context, c Scripter, keys []string, args ...interface{}) *Cmd {
|
||||||
r := s.EvalShaRO(ctx, c, keys, args...)
|
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 s.EvalRO(ctx, c, keys, args...)
|
||||||
}
|
}
|
||||||
return r
|
return r
|
||||||
|
|
Loading…
Reference in New Issue