Skip to content

Commit

Permalink
Set an error returned from the hook on the Cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Feb 2, 2020
1 parent db45a82 commit bdcf7a4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
10 changes: 6 additions & 4 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
35 changes: 35 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package redis_test
import (
"bytes"
"context"
"errors"
"net"
"testing"
"time"

"github.com/go-redis/redis/v7"
Expand All @@ -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

Expand Down

0 comments on commit bdcf7a4

Please sign in to comment.