diff --git a/apisix/plugins/limit-count/init.lua b/apisix/plugins/limit-count/init.lua index ea1d0158e5a1..75d9dcc35356 100644 --- a/apisix/plugins/limit-count/init.lua +++ b/apisix/plugins/limit-count/init.lua @@ -259,7 +259,7 @@ local function gen_limit_key(conf, ctx, key) -- A route which reuses a previous route's ID will inherits its counter. local parent = conf._meta and conf._meta.parent if not parent or not parent.resource_key then - error("failed to generate key invalid parent: ", core.json.encode(parent)) + error("failed to generate key invalid parent: " .. core.json.encode(parent)) end local new_key = parent.resource_key .. ':' .. apisix_plugin.conf_version(conf) diff --git a/t/plugin/limit-count5.t b/t/plugin/limit-count5.t index 15a3e0ba4467..7d07b369c0d2 100644 --- a/t/plugin/limit-count5.t +++ b/t/plugin/limit-count5.t @@ -307,3 +307,47 @@ peek3: 3 commit2: 0 peek4: 0 commit3: rejected + + + +=== TEST 9: error() call in gen_limit_key produces correct error message when parent is missing +--- config + location /t { + content_by_lua_block { + local init = require("apisix.plugins.limit-count.init") + local conf = { + policy = "local", + count = 5, + time_window = 60, + key = "remote_addr", + key_type = "var", + rejected_code = 503, + allow_degradation = false, + show_limit_quota_header = true, + _meta = {} + -- _meta.parent is intentionally missing + } + local ctx = { + var = {remote_addr = "127.0.0.1"}, + conf_version = 1, + conf_type = "route" + } + local ok, err = pcall(init.rate_limit, conf, ctx, "limit-count", 1) + if not ok then + -- error message should contain the JSON of parent (null) + if err:find("failed to generate key invalid parent") then + ngx.say("correct error message") + else + ngx.say("wrong error: " .. tostring(err)) + end + else + ngx.say("should have errored") + end + } + } +--- request +GET /t +--- response_body +correct error message +--- no_error_log +[alert]