Skip to content

Commit 8205bc2

Browse files
committed
FIX: pcall return error table on unknown command
1 parent 6ebaa61 commit 8205bc2

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

cmd_scripting_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,3 +597,28 @@ func TestLuaTX(t *testing.T) {
597597
)
598598
})
599599
}
600+
601+
func TestEvalWithPcall(t *testing.T) {
602+
_, c := runWithClient(t)
603+
604+
t.Run("return error", func(t *testing.T) {
605+
// Test EVAL with pcall and write command (should fail)
606+
mustContain(t, c,
607+
"EVAL", "return redis.pcall('FAKECOMMAND', KEYS[1], ARGV[1])", "1", "key1", "value1",
608+
"Unknown Redis command called from script",
609+
)
610+
})
611+
612+
t.Run("continue after error", func(t *testing.T) {
613+
script := `
614+
local err = redis.pcall('FAKECOMMAND', KEYS[1], ARGV[1]);
615+
local res = "pcall:" .. err['err'];
616+
return res;
617+
`
618+
// Test EVAL with pcall and write command (should fail)
619+
mustContain(t, c,
620+
"EVAL", script, "1", "foo", "value1",
621+
"pcall:ERR Unknown Redis command called from script",
622+
)
623+
})
624+
}

lua.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ func mkLua(srv *server.Server, c *server.Peer, sha string) (map[string]lua.LGFun
7171
return 0
7272
}
7373
// pcall() mode
74-
l.Push(lua.LNil)
74+
res := &lua.LTable{}
75+
if strings.Contains(err.Error(), "ERR unknown command") {
76+
res.RawSetString("err", lua.LString("ERR Unknown Redis command called from script"))
77+
} else {
78+
res.RawSetString("err", lua.LString(err.Error()))
79+
}
80+
l.Push(res)
7581
return 1
7682
}
7783

0 commit comments

Comments
 (0)