Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning JSON responses for common upstream server errors #970

Merged
merged 1 commit into from
Feb 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kong-0.6.1-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ build = {
["kong.core.reports"] = "kong/core/reports.lua",
["kong.core.cluster"] = "kong/core/cluster.lua",
["kong.core.events"] = "kong/core/events.lua",
["kong.core.error_handlers"] = "kong/core/error_handlers.lua",

["kong.dao.cassandra.schema.migrations"] = "kong/dao/cassandra/schema/migrations.lua",
["kong.dao.error"] = "kong/dao/error.lua",
Expand Down
12 changes: 6 additions & 6 deletions kong.yml
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,13 @@ nginx: |
return 200 'User-agent: *\nDisallow: /';
}

error_page 500 /500.html;
location = /500.html {
error_page 500 502 503 504 /50x;

location = /50x {
internal;
content_by_lua '
local responses = require "kong.tools.responses"
responses.send_HTTP_INTERNAL_SERVER_ERROR("An unexpected error occurred")
';
content_by_lua_block {
require("kong.core.error_handlers")(ngx)
}
}
}

Expand Down
52 changes: 52 additions & 0 deletions kong/core/error_handlers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
local constants = require "kong.constants"
local find = string.find
local format = string.format

local TYPE_PLAIN = "text/plain"
local TYPE_JSON = "application/json"
local TYPE_XML = "application/xml"
local TYPE_HTML = "text/html"

local text_template = "%s"
local json_template = '{"message":"%s"}'
local xml_template = '<?xml version="1.0" encoding="UTF-8"?>\n<error><message>%s</message></error>'
local html_template = '<html><head><title>Kong Error</title></head><body><h1>Kong Error</h1><p>%s.</p></body></html>'

local BODIES = {
s500 = "An unexpected error occurred",
s502 = "An invalid response was received from the upstream server",
s503 = "The upstream server is currently unavailable",
s504 = "The upstream server is timing out",
default = "The upstream server responded with %d"
}

local SERVER_HEADER = constants.NAME.."/"..constants.VERSION

return function(ngx)
local accept_header = ngx.req.get_headers()["accept"]
local template, message, content_type

if accept_header == nil then
template = text_template
content_type = TYPE_PLAIN
elseif find(accept_header, TYPE_HTML, nil, true) then
template = html_template
content_type = TYPE_HTML
elseif find(accept_header, TYPE_JSON, nil, true) then
template = json_template
content_type = TYPE_JSON
elseif find(accept_header, TYPE_XML, nil, true) then
template = xml_template
content_type = TYPE_XML
else
template = text_template
content_type = TYPE_PLAIN
end

local status = ngx.status
message = BODIES["s"..status] and BODIES["s"..status] or format(BODIES.default, status)

ngx.header["Server"] = SERVER_HEADER
ngx.header["Content-Type"] = content_type
ngx.say(format(template, message))
end