Repository: go-redis/redis
Pull request #1255 (2020-02-03 10:23:29)

Exported the setErr function as public
resolved: #1254 


Patch:
diff --git a/cluster.go b/cluster.go
index 697c406..ac35ea7 100644
--- a/cluster.go
+++ b/cluster.go
@@ -743,7 +743,7 @@ func (c *ClusterClient) ProcessContext(ctx context.Context, cmd Cmder) error {
 func (c *ClusterClient) process(ctx context.Context, cmd Cmder) error {
 	err := c._process(ctx, cmd)
 	if err != nil {
-		cmd.setErr(err)
+		cmd.SetErr(err)
 		return err
 	}
 	return nil
diff --git a/cluster_commands.go b/cluster_commands.go
index dff62c9..c9b9b9d 100644
--- a/cluster_commands.go
+++ b/cluster_commands.go
@@ -14,7 +14,7 @@ func (c *ClusterClient) DBSize() *IntCmd {
 		return nil
 	})
 	if err != nil {
-		cmd.setErr(err)
+		cmd.SetErr(err)
 		return cmd
 	}
 	cmd.val = size
diff --git a/command.go b/command.go
index b05f82c..0a4a345 100644
--- a/command.go
+++ b/command.go
@@ -20,14 +20,14 @@ type Cmder interface {
 	readTimeout() *time.Duration
 	readReply(rd *proto.Reader) error
 
-	setErr(error)
+	SetErr(error)
 	Err() error
 }
 
 func setCmdsErr(cmds []Cmder, e error) {
 	for _, cmd := range cmds {
 		if cmd.Err() == nil {
-			cmd.setErr(e)
+			cmd.SetErr(e)
 		}
 	}
 }
@@ -119,7 +119,7 @@ func (cmd *baseCmd) stringArg(pos int) string {
 	return s
 }
 
-func (cmd *baseCmd) setErr(e error) {
+func (cmd *baseCmd) SetErr(e error) {
 	cmd.err = e
 }
 
diff --git a/commands.go b/commands.go
index 1e70f93..40d98da 100644
--- a/commands.go
+++ b/commands.go
@@ -2537,7 +2537,7 @@ func (c cmdable) GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd {
 func (c cmdable) GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd {
 	cmd := NewGeoLocationCmd(query, "georadius_ro", key, longitude, latitude)
 	if query.Store != "" || query.StoreDist != "" {
-		cmd.setErr(errors.New("GeoRadius does not support Store or StoreDist"))
+		cmd.SetErr(errors.New("GeoRadius does not support Store or StoreDist"))
 		return cmd
 	}
 	_ = c(cmd)
@@ -2549,7 +2549,7 @@ func (c cmdable) GeoRadiusStore(key string, longitude, latitude float64, query *
 	args := geoLocationArgs(query, "georadius", key, longitude, latitude)
 	cmd := NewIntCmd(args...)
 	if query.Store == "" && query.StoreDist == "" {
-		cmd.setErr(errors.New("GeoRadiusStore requires Store or StoreDist"))
+		cmd.SetErr(errors.New("GeoRadiusStore requires Store or StoreDist"))
 		return cmd
 	}
 	_ = c(cmd)
@@ -2560,7 +2560,7 @@ func (c cmdable) GeoRadiusStore(key string, longitude, latitude float64, query *
 func (c cmdable) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd {
 	cmd := NewGeoLocationCmd(query, "georadiusbymember_ro", key, member)
 	if query.Store != "" || query.StoreDist != "" {
-		cmd.setErr(errors.New("GeoRadiusByMember does not support Store or StoreDist"))
+		cmd.SetErr(errors.New("GeoRadiusByMember does not support Store or StoreDist"))
 		return cmd
 	}
 	_ = c(cmd)
@@ -2572,7 +2572,7 @@ func (c cmdable) GeoRadiusByMemberStore(key, member string, query *GeoRadiusQuer
 	args := geoLocationArgs(query, "georadiusbymember", key, member)
 	cmd := NewIntCmd(args...)
 	if query.Store == "" && query.StoreDist == "" {
-		cmd.setErr(errors.New("GeoRadiusByMemberStore requires Store or StoreDist"))
+		cmd.SetErr(errors.New("GeoRadiusByMemberStore requires Store or StoreDist"))
 		return cmd
 	}
 	_ = c(cmd)
diff --git a/redis.go b/redis.go
index c9a9e17..9cdff78 100644
--- a/redis.go
+++ b/redis.go
@@ -51,14 +51,14 @@ func (hs hooks) process(
 ) error {
 	ctx, err := hs.beforeProcess(ctx, cmd)
 	if err != nil {
-		cmd.setErr(err)
+		cmd.SetErr(err)
 		return err
 	}
 
 	cmdErr := fn(ctx, cmd)
 
 	if err := hs.afterProcess(ctx, cmd); err != nil {
-		cmd.setErr(err)
+		cmd.SetErr(err)
 		return err
 	}
 
@@ -287,7 +287,7 @@ func (c *baseClient) withConn(
 func (c *baseClient) process(ctx context.Context, cmd Cmder) error {
 	err := c._process(ctx, cmd)
 	if err != nil {
-		cmd.setErr(err)
+		cmd.SetErr(err)
 		return err
 	}
 	return nil
diff --git a/result.go b/result.go
index 0878b69..4b7de63 100644
--- a/result.go
+++ b/result.go
@@ -6,7 +6,7 @@ import "time"
 func NewCmdResult(val interface{}, err error) *Cmd {
 	var cmd Cmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -14,7 +14,7 @@ func NewCmdResult(val interface{}, err error) *Cmd {
 func NewSliceResult(val []interface{}, err error) *SliceCmd {
 	var cmd SliceCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -22,7 +22,7 @@ func NewSliceResult(val []interface{}, err error) *SliceCmd {
 func NewStatusResult(val string, err error) *StatusCmd {
 	var cmd StatusCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -30,7 +30,7 @@ func NewStatusResult(val string, err error) *StatusCmd {
 func NewIntResult(val int64, err error) *IntCmd {
 	var cmd IntCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -38,7 +38,7 @@ func NewIntResult(val int64, err error) *IntCmd {
 func NewDurationResult(val time.Duration, err error) *DurationCmd {
 	var cmd DurationCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -46,7 +46,7 @@ func NewDurationResult(val time.Duration, err error) *DurationCmd {
 func NewBoolResult(val bool, err error) *BoolCmd {
 	var cmd BoolCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -54,7 +54,7 @@ func NewBoolResult(val bool, err error) *BoolCmd {
 func NewStringResult(val string, err error) *StringCmd {
 	var cmd StringCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -62,7 +62,7 @@ func NewStringResult(val string, err error) *StringCmd {
 func NewFloatResult(val float64, err error) *FloatCmd {
 	var cmd FloatCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -70,7 +70,7 @@ func NewFloatResult(val float64, err error) *FloatCmd {
 func NewStringSliceResult(val []string, err error) *StringSliceCmd {
 	var cmd StringSliceCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -78,7 +78,7 @@ func NewStringSliceResult(val []string, err error) *StringSliceCmd {
 func NewBoolSliceResult(val []bool, err error) *BoolSliceCmd {
 	var cmd BoolSliceCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -86,7 +86,7 @@ func NewBoolSliceResult(val []bool, err error) *BoolSliceCmd {
 func NewStringStringMapResult(val map[string]string, err error) *StringStringMapCmd {
 	var cmd StringStringMapCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -94,7 +94,7 @@ func NewStringStringMapResult(val map[string]string, err error) *StringStringMap
 func NewStringIntMapCmdResult(val map[string]int64, err error) *StringIntMapCmd {
 	var cmd StringIntMapCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -102,7 +102,7 @@ func NewStringIntMapCmdResult(val map[string]int64, err error) *StringIntMapCmd
 func NewZSliceCmdResult(val []Z, err error) *ZSliceCmd {
 	var cmd ZSliceCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -110,7 +110,7 @@ func NewZSliceCmdResult(val []Z, err error) *ZSliceCmd {
 func NewZWithKeyCmdResult(val *ZWithKey, err error) *ZWithKeyCmd {
 	var cmd ZWithKeyCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -119,7 +119,7 @@ func NewScanCmdResult(keys []string, cursor uint64, err error) *ScanCmd {
 	var cmd ScanCmd
 	cmd.page = keys
 	cmd.cursor = cursor
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -127,7 +127,7 @@ func NewScanCmdResult(keys []string, cursor uint64, err error) *ScanCmd {
 func NewClusterSlotsCmdResult(val []ClusterSlot, err error) *ClusterSlotsCmd {
 	var cmd ClusterSlotsCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -135,7 +135,7 @@ func NewClusterSlotsCmdResult(val []ClusterSlot, err error) *ClusterSlotsCmd {
 func NewGeoLocationCmdResult(val []GeoLocation, err error) *GeoLocationCmd {
 	var cmd GeoLocationCmd
 	cmd.locations = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -143,7 +143,7 @@ func NewGeoLocationCmdResult(val []GeoLocation, err error) *GeoLocationCmd {
 func NewGeoPosCmdResult(val []*GeoPos, err error) *GeoPosCmd {
 	var cmd GeoPosCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -151,7 +151,7 @@ func NewGeoPosCmdResult(val []*GeoPos, err error) *GeoPosCmd {
 func NewCommandsInfoCmdResult(val map[string]*CommandInfo, err error) *CommandsInfoCmd {
 	var cmd CommandsInfoCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -159,7 +159,7 @@ func NewCommandsInfoCmdResult(val map[string]*CommandInfo, err error) *CommandsI
 func NewXMessageSliceCmdResult(val []XMessage, err error) *XMessageSliceCmd {
 	var cmd XMessageSliceCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
 
@@ -167,6 +167,6 @@ func NewXMessageSliceCmdResult(val []XMessage, err error) *XMessageSliceCmd {
 func NewXStreamSliceCmdResult(val []XStream, err error) *XStreamSliceCmd {
 	var cmd XStreamSliceCmd
 	cmd.val = val
-	cmd.setErr(err)
+	cmd.SetErr(err)
 	return &cmd
 }
diff --git a/ring.go b/ring.go
index 20c5e11..0fac576 100644
--- a/ring.go
+++ b/ring.go
@@ -561,7 +561,7 @@ func (c *Ring) cmdShard(cmd Cmder) (*ringShard, error) {
 func (c *Ring) process(ctx context.Context, cmd Cmder) error {
 	err := c._process(ctx, cmd)
 	if err != nil {
-		cmd.setErr(err)
+		cmd.SetErr(err)
 		return err
 	}
 	return nil


Test patch:
diff --git a/command_test.go b/command_test.go
index a0d753e..e9fc958 100644
--- a/command_test.go
+++ b/command_test.go
@@ -1,9 +1,10 @@
 package redis_test
 
 import (
+	"errors"
 	"time"
 
-	"github.com/go-redis/redis/v7"
+	redis "github.com/go-redis/redis/v7"
 
 	. "github.com/onsi/ginkgo"
 	. "github.com/onsi/gomega"
@@ -84,4 +85,12 @@ var _ = Describe("Cmd", func() {
 		Expect(err).NotTo(HaveOccurred())
 		Expect(tm2).To(BeTemporally("==", tm))
 	})
+
+	It("allow to set custom error", func() {
+		e := errors.New("custom error")
+		cmd := redis.Cmd{}
+		cmd.SetErr(e)
+		_, err := cmd.Result()
+		Expect(err).To(Equal(e))
+	})
 })