mirror of
https://github.com/redis/go-redis.git
synced 2025-04-17 20:17:02 +03:00
Handle wrapped errors in scripter.Run (#2674)
* Handle wrapped errors in script * test * remove accidentially committed changes --------- Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com>
This commit is contained in:
parent
84f46c3301
commit
81947daa8d
7
error.go
7
error.go
@ -2,6 +2,7 @@ package redis
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
@ -15,11 +16,11 @@ var ErrClosed = pool.ErrClosed
|
|||||||
|
|
||||||
// HasErrorPrefix checks if the err is a Redis error and the message contains a prefix.
|
// HasErrorPrefix checks if the err is a Redis error and the message contains a prefix.
|
||||||
func HasErrorPrefix(err error, prefix string) bool {
|
func HasErrorPrefix(err error, prefix string) bool {
|
||||||
err, ok := err.(Error)
|
var rErr Error
|
||||||
if !ok {
|
if !errors.As(err, &rErr) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
msg := err.Error()
|
msg := rErr.Error()
|
||||||
msg = strings.TrimPrefix(msg, "ERR ") // KVRocks adds such prefix
|
msg = strings.TrimPrefix(msg, "ERR ") // KVRocks adds such prefix
|
||||||
return strings.HasPrefix(msg, prefix)
|
return strings.HasPrefix(msg, prefix)
|
||||||
}
|
}
|
||||||
|
@ -558,4 +558,24 @@ var _ = Describe("Hook", func() {
|
|||||||
"hook-1-process-end",
|
"hook-1-process-end",
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("wrapped error in a hook", func() {
|
||||||
|
client.AddHook(&hook{
|
||||||
|
processHook: func(hook redis.ProcessHook) redis.ProcessHook {
|
||||||
|
return func(ctx context.Context, cmd redis.Cmder) error {
|
||||||
|
if err := hook(ctx, cmd); err != nil {
|
||||||
|
return fmt.Errorf("wrapped error: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
client.ScriptFlush(ctx)
|
||||||
|
|
||||||
|
script := redis.NewScript(`return 'Script and hook'`)
|
||||||
|
|
||||||
|
cmd := script.Run(ctx, client, nil)
|
||||||
|
Expect(cmd.Err()).NotTo(HaveOccurred())
|
||||||
|
Expect(cmd.Val()).To(Equal("Script and hook"))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user