From 48bb7b133b7ce983a9e4bcfceca3b1e9b7af05cf Mon Sep 17 00:00:00 2001 From: Akulij Date: Tue, 29 Apr 2025 19:36:03 +0300 Subject: [PATCH] make DbError a custom error type to handle internal errors --- src/db/callback_info.rs | 9 +++++---- src/db/mod.rs | 10 +++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/db/callback_info.rs b/src/db/callback_info.rs index ee20982..a0a2ac6 100644 --- a/src/db/callback_info.rs +++ b/src/db/callback_info.rs @@ -49,9 +49,10 @@ where Err(_) => return Ok(None), }; - ci.find_one(doc! { - "_id": id - }) - .await + Ok(ci + .find_one(doc! { + "_id": id + }) + .await?) } } diff --git a/src/db/mod.rs b/src/db/mod.rs index 4ea8bc5..fd9d796 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -147,7 +147,11 @@ impl CallDB for DB { } } -pub type DbError = mongodb::error::Error; +#[derive(thiserror::Error, Debug)] +pub enum DbError { + #[error("error while processing mongodb query: {0}")] + MongodbError(#[from] mongodb::error::Error), +} pub type DbResult = Result; #[async_trait] @@ -159,7 +163,7 @@ pub trait CallDB { let db = self.get_database().await; let users = db.collection::("users"); - users.find(doc! {}).await?.try_collect().await + Ok(users.find(doc! {}).await?.try_collect().await?) } async fn set_admin(&mut self, userid: i64, isadmin: bool) -> DbResult<()> { @@ -279,7 +283,7 @@ pub trait CallDB { let db = self.get_database().await; let events = db.collection::("events"); - events.find(doc! {}).await?.try_collect().await + Ok(events.find(doc! {}).await?.try_collect().await?) } async fn create_event(&mut self, event_datetime: chrono::DateTime) -> DbResult {