Skip to content

Commit

Permalink
Returning JSON responses for common upstream server errors
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Feb 9, 2016
1 parent b708576 commit 676d2ce
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
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

0 comments on commit 676d2ce

Please sign in to comment.