Skip to content

Commit

Permalink
extend cache error
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad committed Aug 4, 2024
1 parent 790c8be commit 8cada1a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 2 additions & 4 deletions examples/demo/src/controllers/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::models::users;
use loco_rs::{cache::CacheError, prelude::*};
use loco_rs::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct CacheResponse {
Expand All @@ -21,9 +21,7 @@ async fn get_or_insert(State(ctx): State<AppContext>) -> Result<Response> {
let res = ctx
.cache
.get_or_insert("user", async {
let user = users::Model::find_by_email(&ctx.db, "user1@example.com")
.await
.map_err(|_| CacheError::Any("user not found".into()))?;
let user = users::Model::find_by_email(&ctx.db, "user1@example.com").await?;
Ok(user.name)
})
.await;
Expand Down
6 changes: 6 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ use self::drivers::CacheDriver;
#[derive(thiserror::Error, Debug)]
#[allow(clippy::module_name_repetitions)]
pub enum CacheError {
#[error(transparent)]
Model(#[from] crate::model::ModelError),

#[error(transparent)]
Loco(#[from] crate::Error),

#[error(transparent)]
Any(#[from] Box<dyn std::error::Error + Send + Sync>),
}
Expand Down

0 comments on commit 8cada1a

Please sign in to comment.