Skip to content

Commit 81947da

Browse files
fred84ofekshenawa
andauthored
Handle wrapped errors in scripter.Run (#2674)
* Handle wrapped errors in script * test * remove accidentially committed changes --------- Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com>
1 parent 84f46c3 commit 81947da

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

error.go

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

33
import (
44
"context"
5+
"errors"
56
"io"
67
"net"
78
"strings"
@@ -15,11 +16,11 @@ var ErrClosed = pool.ErrClosed
1516

1617
// HasErrorPrefix checks if the err is a Redis error and the message contains a prefix.
1718
func HasErrorPrefix(err error, prefix string) bool {
18-
err, ok := err.(Error)
19-
if !ok {
19+
var rErr Error
20+
if !errors.As(err, &rErr) {
2021
return false
2122
}
22-
msg := err.Error()
23+
msg := rErr.Error()
2324
msg = strings.TrimPrefix(msg, "ERR ") // KVRocks adds such prefix
2425
return strings.HasPrefix(msg, prefix)
2526
}

redis_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,4 +558,24 @@ var _ = Describe("Hook", func() {
558558
"hook-1-process-end",
559559
}))
560560
})
561+
562+
It("wrapped error in a hook", func() {
563+
client.AddHook(&hook{
564+
processHook: func(hook redis.ProcessHook) redis.ProcessHook {
565+
return func(ctx context.Context, cmd redis.Cmder) error {
566+
if err := hook(ctx, cmd); err != nil {
567+
return fmt.Errorf("wrapped error: %w", err)
568+
}
569+
return nil
570+
}
571+
},
572+
})
573+
client.ScriptFlush(ctx)
574+
575+
script := redis.NewScript(`return 'Script and hook'`)
576+
577+
cmd := script.Run(ctx, client, nil)
578+
Expect(cmd.Err()).NotTo(HaveOccurred())
579+
Expect(cmd.Val()).To(Equal("Script and hook"))
580+
})
561581
})

0 commit comments

Comments
 (0)