Skip to content

Commit

Permalink
fix: fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
oowl committed Dec 18, 2023
1 parent 6f9cfd0 commit d1c0b12
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
7 changes: 6 additions & 1 deletion kong/db/errors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1032,14 +1032,19 @@ do
for i, err_t_i in drain(section_errors) do
local entity = entities[i]


-- promote error strings to `@entity` type errors
if type(err_t_i) == "string" then
err_t_i = { ["@entity"] = err_t_i }
end

if type(entity) == "table" and type(err_t_i) == "table" then
add_entity_errors(entity_type, entity, err_t_i, flattened)

else
log(WARN, "failed to resolve errors for ", entity_type, " at ",
"index '", i, "'")
section_errors[i] = err_t_i
err_t[entity_type] = section_errors
end
end

Expand Down
32 changes: 7 additions & 25 deletions spec/02-integration/04-admin_api/15-off_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2702,40 +2702,22 @@ R6InCcH2Wh8wSeY5AuDXvu2tv9g/PW9wIJmPuKSHMA==
_format_version = "3.0",
consumers = {
{
id = "a73dc9a7-93df-584d-97c0-7f41a1bbce3d",
username = "test-consumer-1",
},
{
id = "a73dc9a7-93df-584d-97c0-7f41a1bbce32",
username = "test-consumer-1",
},
},
}

local res = client:post("/config?flatten_errors=1", {
body = input,
headers = {
["Content-Type"] = "application/json"
},
})

assert.response(res).has.status(400)
local body = assert.response(res).has.jsonbody()

local errors = body.flattened_errors

assert.not_nil(errors, "`flattened_errors` is missing from the response")
assert.is_table(errors, "`flattened_errors` is not a table")

assert.logfile().has.no.line("[emerg]", true, 0)
assert.logfile().has.no.line("[crit]", true, 0)
assert.logfile().has.no.line("[alert]", true, 0)
assert.logfile().has.no.line("[error]", true, 0)

-- empty flattened errors
assert.equals(0, #errors, "unexpected number of flattened errors")

-- original errors exist
assert.equals(body.fields.consumers[2], "uniqueness violation: 'consumers' entity with username set to 'test-consumer-1' already declared", "unexpected original errors")
local flattened = post_config(input)

assert.equals(1, #flattened,
"unexpected number of flattened errors")
assert.equals("uniqueness violation: 'consumers' entity with username set to 'test-consumer-1' already declared", flattened[1].errors[1].message)
assert.equals("entity", flattened[1].errors[1].type)
end)
end)

Expand Down

0 comments on commit d1c0b12

Please sign in to comment.