Skip to content

Commit 809fc96

Browse files
committed
Handle error in calling code
1 parent 01eb53d commit 809fc96

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

lib/resty/redis/connector.lua

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ local DEFAULTS = setmetatable({
105105
role = "master", -- master | slave | any
106106
sentinels = {},
107107

108-
cluster_startup_nodes = {},
108+
cluster_startup_nodes = {}, -- TODO remove this until implemented?
109109
}, fixed_field_metatable)
110110

111111

@@ -147,38 +147,42 @@ local function parse_dsn(params)
147147
local url = params.url
148148
if url and url ~= "" then
149149
local url_pattern = [[^(?:(redis|sentinel)://)(?:([^@]*)@)?([^:/]+)(?::(\d+|[msa]+))/?(.*)$]]
150-
local m, err = ngx_re_match(url, url_pattern, "")
150+
151+
local m, err = ngx_re_match(url, url_pattern, "oj")
151152
if not m then
152-
ngx_log(ngx_ERR, "could not parse DSN: ", err)
153-
else
154-
local fields
155-
if m[1] == "redis" then
156-
fields = { "password", "host", "port", "db" }
157-
elseif m[1] == "sentinel" then
158-
fields = { "password", "master_name", "role", "db" }
159-
end
153+
return nil, "could not parse DSN: " .. err
154+
end
160155

161-
-- password may not be present
162-
if #m < 5 then tbl_remove(fields, 1) end
156+
local fields
157+
if m[1] == "redis" then
158+
fields = { "password", "host", "port", "db" }
159+
elseif m[1] == "sentinel" then
160+
fields = { "password", "master_name", "role", "db" }
161+
end
163162

164-
local roles = { m = "master", s = "slave", a = "any" }
163+
-- password may not be present
164+
if #m < 5 then tbl_remove(fields, 1) end
165165

166-
for i,v in ipairs(fields) do
167-
params[v] = m[i + 1]
168-
if v == "role" then
169-
params[v] = roles[params[v]]
170-
end
166+
local roles = { m = "master", s = "slave", a = "any" }
167+
168+
for i,v in ipairs(fields) do
169+
params[v] = m[i + 1]
170+
if v == "role" then
171+
params[v] = roles[params[v]]
171172
end
172173
end
173174
end
175+
176+
return true, nil
174177
end
175178

176179

177180
function _M.connect(self, params)
178181
local params = tbl_copy_merge_defaults(params, self.config)
179182

180183
if params.url then
181-
parse_dsn(params)
184+
local ok, err = parse_dsn(params)
185+
if not ok then ngx_log(ngx_ERR, err) end
182186
end
183187

184188
if #params.sentinels > 0 then

0 commit comments

Comments
 (0)