Skip to content

fix(api): map INVALID_FOREIGN_KEY and INVALID_WORKSPACE to 400 - #14964

Open
Adityaj0 wants to merge 1 commit into
Kong:masterfrom
Adityaj0:fix/admin-api-error-code-mapping
Open

fix(api): map INVALID_FOREIGN_KEY and INVALID_WORKSPACE to 400#14964
Adityaj0 wants to merge 1 commit into
Kong:masterfrom
Adityaj0:fix/admin-api-error-code-mapping

Conversation

@Adityaj0

@Adityaj0 Adityaj0 commented Aug 2, 2026

Copy link
Copy Markdown

Summary

Fixes #14963.

kong.db.errors defines INVALID_FOREIGN_KEY and INVALID_WORKSPACE, both of which are actually raised by the DAO layer with safe, actionable, client-facing messages, but neither was present in kong/api/endpoints.lua's ERRORS_HTTP_CODES table. Both codes therefore fell through to the generic if not status or status == 500 branch and were turned into an opaque 500 "An unexpected error occurred", discarding the real error message (which only reached the server logs).

Changes

  • kong/api/endpoints.lua: added INVALID_FOREIGN_KEY and INVALID_WORKSPACE to ERRORS_HTTP_CODES, both mapped to 400, consistent with the neighboring FOREIGN_KEY_VIOLATION / INVALID_PRIMARY_KEY entries already in the table for analogous client-side validation problems.

I did not touch TRANSFORMATION_ERROR (also missing from the table) since it represents a server-side/data-at-rest issue analogous to the already-intentionally-500 DATABASE_ERROR, rather than a client-triggerable, message-safe case like the two above — happy to discuss if maintainers feel it should be included too.

Test plan

  • handle_error is a private local function in endpoints.lua and not covered by an existing unit-test seam; exercising the exact HTTP status without a running Admin API integration test wasn't something I could safely author and verify without a local test-execution environment (see below). The change itself is a minimal, 2-line, same-pattern addition to an existing table.
  • luac -p syntax-checked the changed file.
  • CI (I was not able to run the full busted/OpenResty integration suite in my local environment — please let me know if a targeted Admin API spec would be welcome and I'll add one).

kong.db.errors defines INVALID_FOREIGN_KEY (raised by the DAO when a
unique lookup targets a foreign-typed field with a malformed value,
e.g. via a generic :select_by_<field> Admin API lookup) and
INVALID_WORKSPACE (raised by the postgres strategy on a ws_id foreign
key violation, reachable when a workspace is deleted concurrently with
a write to one of its entities). Neither code was present in
ERRORS_HTTP_CODES, so both fell through to the generic
`if not status or status == 500` branch and were turned into an opaque
500 "An unexpected error occurred", discarding the real, actionable
error message that only reached the server logs.

Both codes represent a client-fixable, 400-class problem, consistent
with the neighboring FOREIGN_KEY_VIOLATION/INVALID_PRIMARY_KEY entries
already in this table.
@CLAassistant

CLAassistant commented Aug 2, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Admin API returns 500 instead of 400 for INVALID_FOREIGN_KEY / INVALID_WORKSPACE errors

2 participants