diff options
| author | Alexander_Kabui <alexanderkabui@proton.me> | 2026-10-13 23:36:39 +0300 |
|---|---|---|
| committer | Alexander_Kabui <alexanderkabui@proton.me> | 2026-10-14 07:28:51 +0300 |
| commit | 22ff53e2d65e47df27d76aa7420a146074c00282 (patch) | |
| tree | 07f550107ebb9501b1fa79e8e8d6b2d48210572d /src/error.rs | |
| parent | a188b75157516c5e00dd130d9ca8dfbd6cf36679 (diff) | |
| download | genenetwork4-22ff53e2d65e47df27d76aa7420a146074c00282.tar.gz | |
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.
Diffstat (limited to 'src/error.rs')
| -rw-r--r-- | src/error.rs | 26 |
1 files changed, 26 insertions, 0 deletions
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 { |
