Skip to content

Commit

Permalink
fix(request-transformer) correctly construct error message when templ…
Browse files Browse the repository at this point in the history
…ate throws Lua

error. That way the errr log will contain actual error messages from the
template closure. Previously the `error` function was used incorrectly
and incorrect Lua error was written into the logs instead. Fixes #25
  • Loading branch information
dndx committed Sep 18, 2020
1 parent 8903d5c commit b478394
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
6 changes: 3 additions & 3 deletions kong/plugins/request-transformer/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ local function iter(config_array)

local res, err = param_value(current_value, config_array)
if err then
return error("[request-transformer] failed to render the template ",
current_value, ", error:", err)
return error("[request-transformer] failed to render the template " ..
current_value .. ", error:" .. err)
end

kong.log.debug("[request-transformer] template `", current_value,
"` rendered to `", res, "`")
"` rendered to `", res, "`")

return i, current_name, res
end, config_array, 0
Expand Down
33 changes: 33 additions & 0 deletions spec/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ describe("Plugin: request-transformer(access) [#" .. strategy .. "]", function()
hosts = { "test23.test" },
paths = { "/request" }
})
local route24 = bp.routes:insert({
hosts = { "test24.test" }
})

bp.plugins:insert {
route = { id = route1.id },
Expand Down Expand Up @@ -380,6 +383,16 @@ describe("Plugin: request-transformer(access) [#" .. strategy .. "]", function()
config = {}
}

bp.plugins:insert {
route = { id = route24.id },
name = "request-transformer",
config = {
add = {
headers = { "x-user-agent:$(type(headers[\"User-Agent\"]) == \"table\" and headers[\"User-Agent\"][1] or headers[\"User-Agent\"])", },
},
}
}

assert(helpers.start_kong({
database = strategy,
plugins = "bundled, request-transformer",
Expand Down Expand Up @@ -1811,6 +1824,26 @@ describe("Plugin: request-transformer(access) [#" .. strategy .. "]", function()
})
assert.response(r).has.status(500)
end)
it("rendering error is correctly propagated in error.log, issue #25", function()
local r = assert(client:send {
method = "GET",
path = "/",
headers = {
host = "test24.test",
}
})
assert.response(r).has.status(500)

helpers.wait_until(function()
local pl_file = require "pl.file"

local cfg = helpers.test_conf
local logs = pl_file.read(cfg.prefix .. "/" .. cfg.proxy_error_log)
local _, count = logs:gsub([[error:%[string "TMP"%]:4: attempt to call global 'type' %(a nil value%)]], "")

return count == 2
end, 5)
end)
it("can inject a value from 'kong.ctx.shared'", function()
local r = assert(client:send {
method = "GET",
Expand Down

0 comments on commit b478394

Please sign in to comment.