Skip to content

Commit 7be00c8

Browse files
authored
fix(panic): Return error instead of panic from commands (#3568)
Instead of panic in few commands, we can return an error to avoid unexpected panics in application code.
1 parent a3a369b commit 7be00c8

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

bitmap_commands.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ func (c cmdable) BitPos(ctx context.Context, key string, bit int64, pos ...int64
141141
args[3] = pos[0]
142142
args[4] = pos[1]
143143
default:
144-
panic("too many arguments")
144+
cmd := NewIntCmd(ctx)
145+
cmd.SetErr(errors.New("too many arguments"))
146+
return cmd
145147
}
146148
cmd := NewIntCmd(ctx, args...)
147149
_ = c(ctx, cmd)
@@ -182,7 +184,9 @@ func (c cmdable) BitFieldRO(ctx context.Context, key string, values ...interface
182184
args[0] = "BITFIELD_RO"
183185
args[1] = key
184186
if len(values)%2 != 0 {
185-
panic("BitFieldRO: invalid number of arguments, must be even")
187+
c := NewIntSliceCmd(ctx)
188+
c.SetErr(errors.New("BitFieldRO: invalid number of arguments, must be even"))
189+
return c
186190
}
187191
for i := 0; i < len(values); i += 2 {
188192
args = append(args, "GET", values[i], values[i+1])

commands.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,9 @@ func (c cmdable) MemoryUsage(ctx context.Context, key string, samples ...int) *I
693693
args := []interface{}{"memory", "usage", key}
694694
if len(samples) > 0 {
695695
if len(samples) != 1 {
696-
panic("MemoryUsage expects single sample count")
696+
cmd := NewIntCmd(ctx)
697+
cmd.SetErr(errors.New("MemoryUsage expects single sample count"))
698+
return cmd
697699
}
698700
args = append(args, "SAMPLES", samples[0])
699701
}

commands_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,9 @@ var _ = Describe("Commands", func() {
735735
n, err = client.MemoryUsage(ctx, "foo", 0).Result()
736736
Expect(err).NotTo(HaveOccurred())
737737
Expect(n).NotTo(BeZero())
738+
739+
_, err = client.MemoryUsage(ctx, "foo", 0, 1).Result()
740+
Expect(err).Should(MatchError("MemoryUsage expects single sample count"))
738741
})
739742
})
740743

@@ -1598,6 +1601,9 @@ var _ = Describe("Commands", func() {
15981601
pos, err = client.BitPos(ctx, "mykey", 0, 0, 0).Result()
15991602
Expect(err).NotTo(HaveOccurred())
16001603
Expect(pos).To(Equal(int64(-1)))
1604+
1605+
_, err = client.BitPos(ctx, "mykey", 0, 0, 0, 0, 0).Result()
1606+
Expect(err).Should(MatchError("too many arguments"))
16011607
})
16021608

16031609
It("should BitPosSpan", func() {
@@ -1635,6 +1641,9 @@ var _ = Describe("Commands", func() {
16351641
nn, err = client.BitFieldRO(ctx, "mykey", "u8", 0, "u4", 8, "u4", 12, "u4", 13).Result()
16361642
Expect(err).NotTo(HaveOccurred())
16371643
Expect(nn).To(Equal([]int64{0, 15, 15, 14}))
1644+
1645+
_, err = client.BitFieldRO(ctx, "mykey", "u8", 0, "u4", 8, "u4", 12, "u4").Result()
1646+
Expect(err).Should(MatchError("BitFieldRO: invalid number of arguments, must be even"))
16381647
})
16391648

16401649
It("should Decr", func() {
@@ -5254,6 +5263,9 @@ var _ = Describe("Commands", func() {
52545263
Score: 1,
52555264
Member: "one",
52565265
}}))
5266+
5267+
_, err = client.ZPopMax(ctx, "zset", 10, 11).Result()
5268+
Expect(err).Should(MatchError("too many arguments"))
52575269
})
52585270

52595271
It("should ZPopMin", func() {
@@ -5321,6 +5333,9 @@ var _ = Describe("Commands", func() {
53215333
Score: 3,
53225334
Member: "three",
53235335
}}))
5336+
5337+
_, err = client.ZPopMin(ctx, "zset", 10, 11).Result()
5338+
Expect(err).Should(MatchError("too many arguments"))
53245339
})
53255340

53265341
It("should ZRange", func() {

sortedset_commands.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package redis
22

33
import (
44
"context"
5+
"errors"
56
"strings"
67
"time"
78

@@ -313,7 +314,9 @@ func (c cmdable) ZPopMax(ctx context.Context, key string, count ...int64) *ZSlic
313314
case 1:
314315
args = append(args, count[0])
315316
default:
316-
panic("too many arguments")
317+
cmd := NewZSliceCmd(ctx)
318+
cmd.SetErr(errors.New("too many arguments"))
319+
return cmd
317320
}
318321

319322
cmd := NewZSliceCmd(ctx, args...)
@@ -333,7 +336,9 @@ func (c cmdable) ZPopMin(ctx context.Context, key string, count ...int64) *ZSlic
333336
case 1:
334337
args = append(args, count[0])
335338
default:
336-
panic("too many arguments")
339+
cmd := NewZSliceCmd(ctx)
340+
cmd.SetErr(errors.New("too many arguments"))
341+
return cmd
337342
}
338343

339344
cmd := NewZSliceCmd(ctx, args...)

0 commit comments

Comments
 (0)