fix(api): map CHECK-constraint violation (empty key) to 422
This commit is contained in:
@@ -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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user