fix(api): map CHECK-constraint violation (empty key) to 422

This commit is contained in:
2026-06-04 14:35:56 +02:00
parent fd1c22191b
commit daad9438ba
2 changed files with 23 additions and 0 deletions
+2
View File
@@ -500,6 +500,8 @@ pub(crate) async fn create_field_definition(
Some("23505") => Err(StatusCode::CONFLICT), Some("23505") => Err(StatusCode::CONFLICT),
// Referenced vocabulary doesn't exist — client error, not server fault. // Referenced vocabulary doesn't exist — client error, not server fault.
Some("23503") => Err(StatusCode::UNPROCESSABLE_ENTITY), Some("23503") => Err(StatusCode::UNPROCESSABLE_ENTITY),
// CHECK constraint violated (e.g. empty key) — client error.
Some("23514") => Err(StatusCode::UNPROCESSABLE_ENTITY),
_ => Err(StatusCode::INTERNAL_SERVER_ERROR), _ => Err(StatusCode::INTERNAL_SERVER_ERROR),
} }
} }
+21
View File
@@ -284,3 +284,24 @@ async fn create_authority_field(pool: PgPool) {
assert_eq!(resp.status(), StatusCode::CREATED); assert_eq!(resp.status(), StatusCode::CREATED);
} }
#[sqlx::test(migrations = "../db/migrations")]
async fn empty_key_is_422(pool: PgPool) {
migrate_sessions(&db::Db::from_pool(pool.clone()))
.await
.unwrap();
seed_user(&pool, "ed@example.com", "pw-editor-123", Role::Editor).await;
let app = build_app(state(pool));
let cookie = login(&app, "ed@example.com", "pw-editor-123").await;
let resp = post_field(
&app,
&cookie,
r#"{"key":"","data_type":"text","required":false,"labels":[{"lang":"en","label":"X"}]}"#,
)
.await;
assert_eq!(resp.status(), StatusCode::UNPROCESSABLE_ENTITY);
}