Set an error returned from the hook on the Cmd

This commit is contained in:
Vladimir Mihailenco 2020-02-02 11:54:42 +02:00
parent db45a825cc
commit bdcf7a49a2
2 changed files with 41 additions and 4 deletions

View File

@ -51,13 +51,14 @@ func (hs hooks) process(
) error {
ctx, err := hs.beforeProcess(ctx, cmd)
if err != nil {
cmd.setErr(err)
return err
}
cmdErr := fn(ctx, cmd)
err = hs.afterProcess(ctx, cmd)
if err != nil {
if err := hs.afterProcess(ctx, cmd); err != nil {
cmd.setErr(err)
return err
}
@ -91,13 +92,14 @@ func (hs hooks) processPipeline(
) error {
ctx, err := hs.beforeProcessPipeline(ctx, cmds)
if err != nil {
setCmdsErr(cmds, err)
return err
}
cmdsErr := fn(ctx, cmds)
err = hs.afterProcessPipeline(ctx, cmds)
if err != nil {
if err := hs.afterProcessPipeline(ctx, cmds); err != nil {
setCmdsErr(cmds, err)
return err
}

View File

@ -3,7 +3,9 @@ package redis_test
import (
"bytes"
"context"
"errors"
"net"
"testing"
"time"
"github.com/go-redis/redis/v7"
@ -12,6 +14,39 @@ import (
. "github.com/onsi/gomega"
)
type redisHookError struct {
redis.Hook
}
var _ redis.Hook = redisHookError{}
func (redisHookError) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
return ctx, nil
}
func (redisHookError) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
return errors.New("hook error")
}
func TestHookError(t *testing.T) {
rdb := redis.NewClient(&redis.Options{
Addr: ":6379",
})
rdb.AddHook(redisHookError{})
err := rdb.Ping().Err()
if err == nil {
t.Fatalf("got nil, expected an error")
}
wanted := "hook error"
if err.Error() != wanted {
t.Fatalf(`got %q, wanted %q`, err, wanted)
}
}
//------------------------------------------------------------------------------
var _ = Describe("Client", func() {
var client *redis.Client