Skip to content

Commit

Permalink
feat(response) add 502 Bad Gateway status code
Browse files Browse the repository at this point in the history
Add a new response type and update the tests to ensure that ngx.log is
called to log the internal error.

From #3427

Signed-off-by: Thibault Charbonnier <[email protected]>
  • Loading branch information
aloisbarreras authored and thibaultcha committed Sep 25, 2018
1 parent 600e848 commit 6257b24
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 10 additions & 4 deletions kong/tools/responses.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ local _M = {
HTTP_CONFLICT = 409,
HTTP_UNSUPPORTED_MEDIA_TYPE = 415,
HTTP_INTERNAL_SERVER_ERROR = 500,
HTTP_BAD_GATEWAY = 502,
HTTP_SERVICE_UNAVAILABLE = 503,
}
}
Expand All @@ -72,6 +73,7 @@ local _M = {
-- @field status_codes.HTTP_UNAUTHORIZED Default: Unauthorized
-- @field status_codes.HTTP_INTERNAL_SERVER_ERROR Always "Internal Server Error"
-- @field status_codes.HTTP_METHOD_NOT_ALLOWED Always "Method not allowed"
-- @field status_codes.HTTP_BAD_GATEWAY Always: "Bad Gateway"
-- @field status_codes.HTTP_SERVICE_UNAVAILABLE Default: "Service unavailable"
local response_default_content = {
[_M.status_codes.HTTP_UNAUTHORIZED] = function(content)
Expand All @@ -89,6 +91,9 @@ local response_default_content = {
[_M.status_codes.HTTP_METHOD_NOT_ALLOWED] = function(content)
return "Method not allowed"
end,
[_M.status_codes.HTTP_BAD_GATEWAY] = function(content)
return "Bad Gateway"
end,
[_M.status_codes.HTTP_SERVICE_UNAVAILABLE] = function(content)
return content or "Service unavailable"
end,
Expand Down Expand Up @@ -116,10 +121,11 @@ local function send_response(status_code)
coroutine.yield()
end

if status_code == _M.status_codes.HTTP_INTERNAL_SERVER_ERROR then
if content then
ngx.log(ngx.ERR, tostring(content))
end
if (status_code == _M.status_codes.HTTP_INTERNAL_SERVER_ERROR
or status_code == _M.status_codes.HTTP_BAD_GATEWAY)
and content ~= nil
then
ngx.log(ngx.ERR, tostring(content))
end

ngx.status = status_code
Expand Down
8 changes: 7 additions & 1 deletion spec/01-unit/009-responses_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("Response helpers", function()
end)
end
end)
it("calls `ngx.log` if and only if a 500 status code was given", function()
it("calls `ngx.log` if 500 or 502 status code was given", function()
responses.send_HTTP_BAD_REQUEST()
assert.stub(ngx.log).was_not_called()

Expand All @@ -69,8 +69,14 @@ describe("Response helpers", function()
responses.send_HTTP_INTERNAL_SERVER_ERROR()
assert.stub(ngx.log).was_not_called()

responses.send_HTTP_BAD_GATEWAY()
assert.stub(ngx.log).was_not_called()

responses.send_HTTP_INTERNAL_SERVER_ERROR("error")
assert.stub(ngx.log).was_called()

responses.send_HTTP_BAD_GATEWAY("error")
assert.stub(ngx.log).was_called(2)
end)

it("don't call `ngx.log` if a 503 status code was given", function()
Expand Down

0 comments on commit 6257b24

Please sign in to comment.