Skip to content

Commit

Permalink
Merge pull request #325 from pkierski/fix_error_reply
Browse files Browse the repository at this point in the history
fix: handle messages with custom error code in error_reply
  • Loading branch information
alicebob committed Apr 24, 2023
2 parents d807499 + b071b9a commit 1be35fa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
7 changes: 7 additions & 0 deletions integration/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ func TestLua(t *testing.T) {
c.Error("type of arguments", "EVAL", "return redis.status_reply(1)", "0")
c.Error("type of arguments", "EVAL", "return redis.status_reply()", "0")
c.Error("type of arguments", "EVAL", "return redis.status_reply(redis.status_reply('foo'))", "0")

c.ErrorTheSame("ERR ", "EVAL", "return redis.error_reply('')", "0")
c.ErrorTheSame("ERR ", "EVAL", "return redis.error_reply('-')", "0")
c.ErrorTheSame("ERR foo", "EVAL", "return redis.error_reply('foo')", "0")
c.ErrorTheSame("ERR foo", "EVAL", "return redis.error_reply('-foo')", "0")
c.ErrorTheSame("foo bar", "EVAL", "return redis.error_reply('foo bar')", "0")
c.ErrorTheSame("foo bar", "EVAL", "return redis.error_reply('-foo bar')", "0")
})

// state inside lua
Expand Down
36 changes: 36 additions & 0 deletions integration/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,42 @@ func (c *client) Error(msg string, cmd string, args ...string) {
// }
}

// both must return exactly the same error
func (c *client) ErrorTheSame(msg string, cmd string, args ...string) {
c.t.Helper()

resReal, errReal := c.real.Do(append([]string{cmd}, args...)...)
if errReal != nil {
c.t.Errorf("error from realredis: %s", errReal)
return
}
resMini, errMini := c.mini.Do(append([]string{cmd}, args...)...)
if errMini != nil {
c.t.Errorf("error from miniredis: %s", errMini)
return
}

mini, err := proto.ReadError(resMini)
if err != nil {
c.t.Logf("real:%q mini:%q", string(resReal), string(resMini))
c.t.Errorf("parse error miniredis: %s", err)
return
}
real, err := proto.ReadError(resReal)
if err != nil {
c.t.Errorf("parse error realredis: %s", err)
return
}

if real != msg {
c.t.Errorf("expected (real)\n%q\nto contain %q", real, msg)
}
if mini != msg {
c.t.Errorf("expected (mini)\n%q\nto contain %q\nreal:\n%s", mini, msg, real)
}
// real == msg && mini == msg => real == mini, so we don't want to check it explicity
}

// only receive a command, which can't be an error
func (c *client) Receive() {
c.t.Helper()
Expand Down
13 changes: 12 additions & 1 deletion lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,18 @@ func mkLua(srv *server.Server, c *server.Peer, sha string) (map[string]lua.LGFun
return 0
}
res := &lua.LTable{}
res.RawSetString("err", lua.LString("ERR "+msg))
parts := strings.SplitN(msg.String(), " ", 2)
// '-' at the beginging will be added as a part of error response
if parts[0] != "" && parts[0][0] == '-' {
parts[0] = parts[0][1:]
}
var final_msg string
if len(parts) == 2 {
final_msg = fmt.Sprintf("%s %s", parts[0], parts[1])
} else {
final_msg = fmt.Sprintf("ERR %s", parts[0])
}
res.RawSetString("err", lua.LString(final_msg))
l.Push(res)
return 1
},
Expand Down

0 comments on commit 1be35fa

Please sign in to comment.