make DbError a custom error type to handle internal errors

This commit is contained in:
Akulij 2025-04-29 19:36:03 +03:00
parent 221fb87c8f
commit 48bb7b133b
2 changed files with 12 additions and 7 deletions

View File

@ -49,9 +49,10 @@ where
Err(_) => return Ok(None), Err(_) => return Ok(None),
}; };
ci.find_one(doc! { Ok(ci
.find_one(doc! {
"_id": id "_id": id
}) })
.await .await?)
} }
} }

View File

@ -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<T> = Result<T, DbError>; pub type DbResult<T> = Result<T, DbError>;
#[async_trait] #[async_trait]
@ -159,7 +163,7 @@ pub trait CallDB {
let db = self.get_database().await; let db = self.get_database().await;
let users = db.collection::<User>("users"); let users = db.collection::<User>("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<()> { 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 db = self.get_database().await;
let events = db.collection::<Event>("events"); let events = db.collection::<Event>("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<Utc>) -> DbResult<Event> { async fn create_event(&mut self, event_datetime: chrono::DateTime<Utc>) -> DbResult<Event> {