From 22ff53e2d65e47df27d76aa7420a146074c00282 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Tue, 13 Oct 2026 23:36:39 +0300 Subject: config: GN4_DB_MODE (mariadb optional on dev) + no-DB hardening * src/config.rs (DbMode, Config.db_mode, parse_db_mode): add GN4_DB_MODE=dev|production in the layered config; dev runs with no configured DB; fail-closed parse (a typo refuses). * src/db.rs (DbError, pool, probe), src/error.rs (ApiError::from_db): mint a typed 503 db_unavailable at the db::pool() choke point, distinguishing Unavailable from Query. * src/main.rs: loud no-DB startup line; production refuses to start (exit non-zero) before axum::serve. * src/routes/{accessions,datasets,groups,mapping_methods,publications,species,traits}.rs: route every DB error through ApiError::from_db. * src/bin/gn-indexer.rs: refuse to build an index when the DB is unreachable. * tests/no_db.rs: pin no-DB degradation (search 200 partial, DB routes 503). His ask (DM 2026-10-12): "Make it an option to switch it off for development machines? I mean mariadb." The no-DB hardening is ours (review-driven). Default GN4_DB_MODE left unflipped (=dev); the flip needs owner sign-off. --- src/error.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/error.rs') diff --git a/src/error.rs b/src/error.rs index 3f9479a..7daa103 100644 --- a/src/error.rs +++ b/src/error.rs @@ -111,6 +111,32 @@ impl ApiError { self.next = Some(next.to_string()); self } + + /// Map a database failure onto the shared error contract. An + /// unreachable/unconfigured database (minted at `db::pool()`) is a + /// typed 503 `db_unavailable` -- the database itself is down, not + /// this request; a query failure on an established pool keeps the + /// existing 500 `database_error`. Every DB-touching route maps + /// through this one place, so all four `From`-adapter shapes and + /// the inline `map_err` sites agree. + pub fn from_db(error: crate::db::DbError, operation_id: &str) -> ApiError { + match error { + crate::db::DbError::Unavailable(detail) => ApiError::new( + StatusCode::SERVICE_UNAVAILABLE, + "db_unavailable", + "Database unavailable", + detail, + operation_id, + ), + crate::db::DbError::Query(error) => ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "database_error", + "Database error", + error.to_string(), + operation_id, + ), + } + } } impl IntoResponse for ApiError { -- cgit 1.4.1