about summary refs log tree commit diff
path: root/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs26
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 {