nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: go-redis/redis
Pull request #1088 (2019-07-11 09:04:14)

Don't return a context from After* hooks


Patch:
diff --git a/redis.go b/redis.go
index 31dbea3..10200bb 100644
--- a/redis.go
+++ b/redis.go
@@ -22,10 +22,10 @@ func SetLogger(logger *log.Logger) {
 
 type Hook interface {
 	BeforeProcess(ctx context.Context, cmd Cmder) (context.Context, error)
-	AfterProcess(ctx context.Context, cmd Cmder) (context.Context, error)
+	AfterProcess(ctx context.Context, cmd Cmder) error
 
 	BeforeProcessPipeline(ctx context.Context, cmds []Cmder) (context.Context, error)
-	AfterProcessPipeline(ctx context.Context, cmds []Cmder) (context.Context, error)
+	AfterProcessPipeline(ctx context.Context, cmds []Cmder) error
 }
 
 type hooks struct {
@@ -68,8 +68,7 @@ func (hs hooks) beforeProcess(ctx context.Context, cmd Cmder) (context.Context,
 func (hs hooks) afterProcess(ctx context.Context, cmd Cmder) (context.Context, error) {
 	var firstErr error
 	for _, h := range hs.hooks {
-		var err error
-		ctx, err = h.AfterProcess(ctx, cmd)
+		err := h.AfterProcess(ctx, cmd)
 		if err != nil && firstErr == nil {
 			firstErr = err
 		}
@@ -109,8 +108,7 @@ func (hs hooks) beforeProcessPipeline(ctx context.Context, cmds []Cmder) (contex
 func (hs hooks) afterProcessPipeline(ctx context.Context, cmds []Cmder) (context.Context, error) {
 	var firstErr error
 	for _, h := range hs.hooks {
-		var err error
-		ctx, err = h.AfterProcessPipeline(ctx, cmds)
+		err := h.AfterProcessPipeline(ctx, cmds)
 		if err != nil && firstErr == nil {
 			firstErr = err
 		}


Test patch:
diff --git a/example_instrumentation_test.go b/example_instrumentation_test.go
index 65ef59c..e128ae3 100644
--- a/example_instrumentation_test.go
+++ b/example_instrumentation_test.go
@@ -16,9 +16,9 @@ func (redisHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Co
 	return ctx, nil
 }
 
-func (redisHook) AfterProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
+func (redisHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
 	fmt.Printf("finished processing: <%s>\n", cmd)
-	return ctx, nil
+	return nil
 }
 
 func (redisHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
@@ -26,9 +26,9 @@ func (redisHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder)
 	return ctx, nil
 }
 
-func (redisHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
+func (redisHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error {
 	fmt.Printf("pipeline finished processing: %v\n", cmds)
-	return ctx, nil
+	return nil
 }
 
 func Example_instrumentation() {
Repository: go-redis/redis
Pull request #1251 (2020-02-02 09:55:53)

Set an error returned from the hook on the Cmd
Fixes https://github.com/go-redis/redis/issues/1214


Patch:
diff --git a/redis.go b/redis.go
index 123e64d..66dc72f 100644
--- a/redis.go
+++ b/redis.go
@@ -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
 	}
 


Test patch:
diff --git a/redis_test.go b/redis_test.go
index b74281d..8a1a149 100644
--- a/redis_test.go
+++ b/redis_test.go
@@ -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