Skip to content

Commit

Permalink
Handle wrapped errors in scripter.Run (#2674)
Browse files Browse the repository at this point in the history
* Handle wrapped errors in script

* test

* remove accidentially committed changes

---------

Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com>
  • Loading branch information
fred84 and ofekshenawa committed Oct 30, 2023
1 parent 84f46c3 commit 81947da
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
7 changes: 4 additions & 3 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package redis

import (
"context"
"errors"
"io"
"net"
"strings"
Expand All @@ -15,11 +16,11 @@ var ErrClosed = pool.ErrClosed

// HasErrorPrefix checks if the err is a Redis error and the message contains a prefix.
func HasErrorPrefix(err error, prefix string) bool {
err, ok := err.(Error)
if !ok {
var rErr Error
if !errors.As(err, &rErr) {
return false
}
msg := err.Error()
msg := rErr.Error()
msg = strings.TrimPrefix(msg, "ERR ") // KVRocks adds such prefix
return strings.HasPrefix(msg, prefix)
}
Expand Down
20 changes: 20 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,4 +558,24 @@ var _ = Describe("Hook", func() {
"hook-1-process-end",
}))
})

It("wrapped error in a hook", func() {
client.AddHook(&hook{
processHook: func(hook redis.ProcessHook) redis.ProcessHook {
return func(ctx context.Context, cmd redis.Cmder) error {
if err := hook(ctx, cmd); err != nil {
return fmt.Errorf("wrapped error: %w", err)
}
return nil
}
},
})
client.ScriptFlush(ctx)

script := redis.NewScript(`return 'Script and hook'`)

cmd := script.Run(ctx, client, nil)
Expect(cmd.Err()).NotTo(HaveOccurred())
Expect(cmd.Val()).To(Equal("Script and hook"))
})
})

0 comments on commit 81947da

Please sign in to comment.