Skip to content

Commit

Permalink
[utils] append the original error to the error string
Browse files Browse the repository at this point in the history
  • Loading branch information
mayorova authored and mikz committed Jun 26, 2018
1 parent 46212ce commit f299e01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 5 additions & 5 deletions gateway/src/apicast/threescale_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,22 @@ function _M.connect_redis(options)

local ok, err = red:connect(_M.resolve(host, port))
if not ok then
return nil, _M.error("failed to connect to redis on " .. host .. ":" .. port .. ":", err)
return nil, _M.error("failed to connect to redis on ", host, ":", port, ": ", err)
end

if opts.password then
ok = red:auth(opts.password)

if not ok then
return nil, _M.error("failed to auth on redis " .. host .. ":" .. port)
return nil, _M.error("failed to auth on redis ", host, ":", port)
end
end

if opts.db then
ok = red:select(opts.db)

if not ok then
return nil, _M.error("failed to select db " .. opts.db .. " on redis " .. host .. ":" .. port)
return nil, _M.error("failed to select db ", opts.db, " on redis ", host, ":", port)
end
end

Expand All @@ -187,10 +187,10 @@ function _M.match_xml_element(xml, element, value)
return string.find(xml, pattern, xml_header_len, xml_header_len, true)
end

-- error and exist
-- error and exit
function _M.error(...)
if ngx.get_phase() == 'timer' then
return ...
return table.concat(table.pack(...))
else
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
ngx.say(...)
Expand Down
14 changes: 14 additions & 0 deletions spec/threescale_utils_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local _M = require('apicast.threescale_utils')

describe('3scale utils', function()
describe('.error', function()
it('returns concatenated error in timer phase', function()
local get_phase = spy.on(ngx, 'get_phase', function() return 'timer' end)
local error = _M.error('one', ' two', ' three')

assert.spy(get_phase).was_called(1)

assert.equal('one two three', error)
end)
end)
end)

0 comments on commit f299e01

Please sign in to comment.