File tree Expand file tree Collapse file tree 2 files changed +41
-4
lines changed Expand file tree Collapse file tree 2 files changed +41
-4
lines changed Original file line number Diff line number Diff line change @@ -51,13 +51,14 @@ func (hs hooks) process(
51
51
) error {
52
52
ctx , err := hs .beforeProcess (ctx , cmd )
53
53
if err != nil {
54
+ cmd .setErr (err )
54
55
return err
55
56
}
56
57
57
58
cmdErr := fn (ctx , cmd )
58
59
59
- err = hs .afterProcess (ctx , cmd )
60
- if err != nil {
60
+ if err : = hs .afterProcess (ctx , cmd ); err != nil {
61
+ cmd . setErr ( err )
61
62
return err
62
63
}
63
64
@@ -91,13 +92,14 @@ func (hs hooks) processPipeline(
91
92
) error {
92
93
ctx , err := hs .beforeProcessPipeline (ctx , cmds )
93
94
if err != nil {
95
+ setCmdsErr (cmds , err )
94
96
return err
95
97
}
96
98
97
99
cmdsErr := fn (ctx , cmds )
98
100
99
- err = hs .afterProcessPipeline (ctx , cmds )
100
- if err != nil {
101
+ if err : = hs .afterProcessPipeline (ctx , cmds ); err != nil {
102
+ setCmdsErr ( cmds , err )
101
103
return err
102
104
}
103
105
Original file line number Diff line number Diff line change @@ -3,7 +3,9 @@ package redis_test
3
3
import (
4
4
"bytes"
5
5
"context"
6
+ "errors"
6
7
"net"
8
+ "testing"
7
9
"time"
8
10
9
11
"github.com/go-redis/redis/v7"
@@ -12,6 +14,39 @@ import (
12
14
. "github.com/onsi/gomega"
13
15
)
14
16
17
+ type redisHookError struct {
18
+ redis.Hook
19
+ }
20
+
21
+ var _ redis.Hook = redisHookError {}
22
+
23
+ func (redisHookError ) BeforeProcess (ctx context.Context , cmd redis.Cmder ) (context.Context , error ) {
24
+ return ctx , nil
25
+ }
26
+
27
+ func (redisHookError ) AfterProcess (ctx context.Context , cmd redis.Cmder ) error {
28
+ return errors .New ("hook error" )
29
+ }
30
+
31
+ func TestHookError (t * testing.T ) {
32
+ rdb := redis .NewClient (& redis.Options {
33
+ Addr : ":6379" ,
34
+ })
35
+ rdb .AddHook (redisHookError {})
36
+
37
+ err := rdb .Ping ().Err ()
38
+ if err == nil {
39
+ t .Fatalf ("got nil, expected an error" )
40
+ }
41
+
42
+ wanted := "hook error"
43
+ if err .Error () != wanted {
44
+ t .Fatalf (`got %q, wanted %q` , err , wanted )
45
+ }
46
+ }
47
+
48
+ //------------------------------------------------------------------------------
49
+
15
50
var _ = Describe ("Client" , func () {
16
51
var client * redis.Client
17
52
You can’t perform that action at this time.
0 commit comments