diff --git a/kong/init.lua b/kong/init.lua index 2a50df65fa5..542b7912f17 100644 --- a/kong/init.lua +++ b/kong/init.lua @@ -277,6 +277,9 @@ local function execute_access_plugins_iterator(plugins_iterator, ctx) status_code = 500, content = { message = "An unexpected error occurred" }, } + + -- plugin that throws runtime exception should be marked as `error` + ctx.KONG_UNEXPECTED = true end reset_plugin_context(ctx, old_ws) diff --git a/spec/02-integration/05-proxy/04-plugins_triggering_spec.lua b/spec/02-integration/05-proxy/04-plugins_triggering_spec.lua index e0384f0c2e6..d0e8b910864 100644 --- a/spec/02-integration/05-proxy/04-plugins_triggering_spec.lua +++ b/spec/02-integration/05-proxy/04-plugins_triggering_spec.lua @@ -675,6 +675,37 @@ for _, strategy in helpers.each_strategy() do } end + do + -- plugin to mock runtime exception + local mock_one_fn = [[ + local nilValue = nil + kong.log.info('test' .. nilValue) + ]] + + local mock_two_fn = [[ + ngx.header['X-Source'] = kong.response.get_source() + ]] + + local mock_service = bp.services:insert { + name = "runtime_exception", + } + + bp.routes:insert { + hosts = { "runtime_exception" }, + protocols = { "http" }, + service = mock_service, + } + + bp.plugins:insert { + name = "pre-function", + service = { id = mock_service.id }, + config = { + ["access"] = { mock_one_fn }, + ["header_filter"] = { mock_two_fn }, + }, + } + end + do -- global plugin to catch Nginx-produced client errors bp.plugins:insert { @@ -1022,6 +1053,19 @@ for _, strategy in helpers.each_strategy() do assert.res_status(504, res) -- Gateway Timeout assert.equal("timeout", res.headers["Log-Plugin-Service-Matched"]) end) + + it("kong.response.get_source() returns \"error\" if plugin runtime exception occurs, FTI-3200", function() + local res = assert(proxy_client:send { + method = "GET", + path = "/status/200", + headers = { + ["Host"] = "runtime_exception" + } + }) + local body = assert.res_status(500, res) + assert.same("body_filter", body) + assert.equal("error", res.headers["X-Source"]) + end) end) describe("plugin's init_worker", function()