Skip to content

Commit e830162

Browse files
piotrppintsized
authored andcommitted
Return correct error when DB selection fails
1 parent cdd8523 commit e830162

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

lib/resty/redis/connector.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local ngx_log = ngx.log
55
local ngx_ERR = ngx.ERR
66
local ngx_re_match = ngx.re.match
77

8+
local str_find = string.find
89
local tbl_remove = table.remove
910
local tbl_sort = table.sort
1011
local ok, tbl_new = pcall(require, "table.new")
@@ -356,14 +357,24 @@ function _M.connect_to_host(self, host)
356357
if password and password ~= "" then
357358
local res, err = r:auth(password)
358359
if err then
359-
ngx_log(ngx_ERR, err)
360360
return res, err
361361
end
362362
end
363363

364364
-- No support for DBs in proxied Redis.
365365
if config.connection_is_proxied ~= true and host.db ~= nil then
366-
r:select(host.db)
366+
local res, err = r:select(host.db)
367+
-- SELECT will fail if we are connected to sentinel:
368+
-- detect it and ignore error message it that's the case
369+
if err and str_find(err, "ERR unknown command") then
370+
local role = r:role()
371+
if role and role[1] == "sentinel" then
372+
err = nil
373+
end
374+
end
375+
if err then
376+
return res, err
377+
end
367378
end
368379
return r, nil
369380
end

t/connector.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ location /t {
210210
}
211211
--- request
212212
GET /t
213-
--- error_log
214-
ERR Client sent AUTH, but no password is set
213+
--- no_error_log
214+
[error]
215215

216216

217217
=== TEST 7: Bad unix domain socket path should fail

t/sentinel.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ location /t {
3232
local rc = require("resty.redis.connector").new()
3333
3434
local sentinel, err = rc:connect{ url = "redis://127.0.0.1:$TEST_NGINX_SENTINEL_PORT1" }
35-
assert(sentinel and not err, "sentinel should connect without errors")
35+
assert(sentinel and not err, "sentinel should connect without errors but got " .. tostring(err))
3636
3737
local master, err = require("resty.redis.sentinel").get_master(
3838
sentinel,

0 commit comments

Comments
 (0)