Return 404 (not 500) when adding a term to a non-existent vocabulary #22

Closed
opened 2026-06-02 21:02:02 +00:00 by logaritmisk · 0 comments
Owner

Context

POST /api/admin/vocabularies/{id}/terms (add_term in crates/api/src/admin_vocab.rs, merged at c4e0c4c) parses {id} as a VocabularyId and, if the UUID is well-formed but no such vocabulary exists, lets the insert hit a foreign-key violation. That FK error is mapped to 500 Internal Server Error.

A well-formed id pointing at a missing vocabulary is a client error, so the correct status is 404 Not Found (consistent with how the object/term/authority endpoints treat missing/unparseable ids elsewhere). Behaviour is safe today — the transaction rolls back — only the status code is coarse.

Task

  • In add_term, pre-check vocabulary existence (e.g. db::vocab::vocabulary_by_key/an existence query, or distinguish the FK violation from other sqlx::Errors) and return 404 when the vocabulary is absent.
  • Keep 500 only for genuine DB failures.

Acceptance

  • POST /api/admin/vocabularies/<valid-but-unknown-uuid>/terms404.
  • Adding a term to an existing vocabulary still → 201.
  • A test covers the missing-vocabulary case.
## Context `POST /api/admin/vocabularies/{id}/terms` (`add_term` in `crates/api/src/admin_vocab.rs`, merged at `c4e0c4c`) parses `{id}` as a `VocabularyId` and, if the UUID is well-formed but no such vocabulary exists, lets the insert hit a foreign-key violation. That FK error is mapped to **500 Internal Server Error**. A well-formed id pointing at a missing vocabulary is a **client** error, so the correct status is **404 Not Found** (consistent with how the object/term/authority endpoints treat missing/unparseable ids elsewhere). Behaviour is safe today — the transaction rolls back — only the status code is coarse. ## Task - In `add_term`, pre-check vocabulary existence (e.g. `db::vocab::vocabulary_by_key`/an existence query, or distinguish the FK violation from other `sqlx::Error`s) and return `404` when the vocabulary is absent. - Keep `500` only for genuine DB failures. ## Acceptance - `POST /api/admin/vocabularies/<valid-but-unknown-uuid>/terms` → `404`. - Adding a term to an existing vocabulary still → `201`. - A test covers the missing-vocabulary case.
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: logaritmisk/biggus-dickus#22