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

feat(proxy): make error responses content type compliant with request header values #10366

Merged
merged 3 commits into from
Feb 28, 2023
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@

## Unreleased

### Additions

#### Core

- Make runloop and init error response content types compliant with Accept header value
[#10366](https://github.com/Kong/kong/pull/10366)
### Dependencies

- Bumped lua-resty-session from 4.0.2 to 4.0.3
Expand Down
8 changes: 4 additions & 4 deletions kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,9 @@ local function flush_delayed_response(ctx)
return -- avoid tail call
end

kong.response.exit(ctx.delayed_response.status_code,
ctx.delayed_response.content,
ctx.delayed_response.headers)
local dr = ctx.delayed_response
local message = dr.content and dr.content.message or dr.content
kong.response.error(dr.status_code, message, dr.headers)
end


Expand Down Expand Up @@ -1005,7 +1005,7 @@ function Kong.access()

ctx.buffered_proxying = nil

return kong.response.exit(503, { message = "no Service found with those values"})
return kong.response.error(503, "no Service found with those values")
end

runloop.access.after(ctx)
Expand Down
14 changes: 6 additions & 8 deletions kong/runloop/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1079,8 +1079,7 @@ return {
after = function(ctx)
local ok, err, errcode = balancer_execute(ctx)
if not ok then
local body = utils.get_default_exit_body(errcode, err)
return kong.response.exit(errcode, body)
return kong.response.error(errcode, err)
end
end
},
Expand Down Expand Up @@ -1118,7 +1117,7 @@ return {
span:finish()
end

return kong.response.exit(404, { message = "no Route matched with those values" })
return kong.response.error(404, "no Route matched with those values")
end

-- ends tracing span
Expand Down Expand Up @@ -1185,7 +1184,7 @@ return {
local redirect_status_code = route.https_redirect_status_code or 426

if redirect_status_code == 426 then
return kong.response.exit(426, { message = "Please use HTTPS protocol" }, {
return kong.response.error(426, "Please use HTTPS protocol", {
["Connection"] = "Upgrade",
["Upgrade"] = "TLS/1.2, HTTP/1.1",
})
Expand All @@ -1209,15 +1208,15 @@ return {
if content_type and sub(content_type, 1, #"application/grpc") == "application/grpc" then
if protocol_version ~= 2 then
-- mismatch: non-http/2 request matched grpc route
return kong.response.exit(426, { message = "Please use HTTP2 protocol" }, {
return kong.response.error(426, "Please use HTTP2 protocol", {
["connection"] = "Upgrade",
["upgrade"] = "HTTP/2",
})
end

else
-- mismatch: non-grpc request matched grpc route
return kong.response.exit(415, { message = "Non-gRPC request matched gRPC route" })
return kong.response.error(415, "Non-gRPC request matched gRPC route")
end

if not protocols.grpc and forwarded_proto ~= "https" then
Expand Down Expand Up @@ -1339,8 +1338,7 @@ return {

local ok, err, errcode = balancer_execute(ctx)
if not ok then
local body = utils.get_default_exit_body(errcode, err)
return kong.response.exit(errcode, body)
return kong.response.error(errcode, err)
end

local ok, err = balancer.set_host_header(balancer_data, upstream_scheme, upstream_host)
Expand Down
2 changes: 1 addition & 1 deletion spec/01-unit/16-runloop_handler_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ local function setup_it_block()
warn = function() end,
},
response = {
exit = function() end,
error = function() end,
},
worker_events = {
register = function() end,
Expand Down
68 changes: 68 additions & 0 deletions spec/02-integration/05-proxy/29-collect-plugin-errors_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"

for _, strategy in helpers.each_strategy() do
describe("Collect plugin errors [#" .. strategy .. "]", function()
local client

lazy_setup(function()
local bp = helpers.get_db_utils(strategy, {
"routes",
"services",
"plugins",
},{
"logger"
})

local service = assert(bp.services:insert {
url = helpers.mock_upstream_url
})

local route = assert(bp.routes:insert {
service = service,
hosts = { "error.test" }
})

assert(bp.plugins:insert {
name = "error-generator",
route = { id = route.id },
config = {
access = true,
},
})
assert(bp.plugins:insert {
name = "logger",
route = { id = route.id },
})

assert(helpers.start_kong({
database = strategy,
plugins = "bundled, error-generator, logger",
nginx_conf = "spec/fixtures/custom_nginx.template",
}))
client = helpers.proxy_client()
end)

lazy_teardown(function()
if client then
client:close()
end
helpers.stop_kong()
end)

it("delays the error response", function()
local res = assert(client:get("/get", {
headers = {
Host = "error.test",
}
}))
local body = assert.res_status(500, res)
local json = cjson.decode(body)
assert.same({ message = "An unexpected error occurred" }, json)
-- the other plugin's phases were executed:
assert.logfile().has.line("header_filter phase", true)
assert.logfile().has.line("body_filter phase", true)
assert.logfile().has.line("log phase", true)
end)
end)
end
3 changes: 2 additions & 1 deletion spec/03-plugins/33-serverless-functions/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ for _, plugin_name in ipairs({ "pre-function", "post-function" }) do
}
})
local body = assert.res_status(500, res)
assert.same('{"message":"An unexpected error occurred"}', body)
local json = cjson.decode(body)
assert.same({ message = "An unexpected error occurred" }, json)
end)
end)

Expand Down