From b2684d93482cec8e40cd6ccbc2cee4be3b1eef25 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 20 Aug 2021 18:35:22 +0530 Subject: [PATCH] handle errors within System methods --- CHANGELOG.md | 3 +++ examples/simple.rs | 2 +- src/errors.rs | 16 ++++++++++++++++ src/lib.rs | 2 +- src/system.rs | 37 ++++++++++++------------------------- 5 files changed, 33 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6f39da..1fe4122 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,9 @@ `crate::master::embedded::master`, it automatically starts `Counter` actor. +- `System::get_pow` returns `errors::CaptchaResult>` + instead of `Option` + ## 0.1.3 ## Added diff --git a/examples/simple.rs b/examples/simple.rs index 187090f..cda3931 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -90,7 +90,7 @@ async fn main() -> std::io::Result<()> { // Get PoW config. Should be called everytime there's a visitor for a // managed site(here mcaptcha_name) - let work_req = system.get_pow(mcaptcha_name.into()).await.unwrap(); + let work_req = system.get_pow(mcaptcha_name.into()).await.unwrap().unwrap(); // the following computation should be done on the client but for the purpose // of this illustration, we are going to do it on the server it self diff --git a/src/errors.rs b/src/errors.rs index fddfd1d..5dd04ea 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -20,6 +20,8 @@ use derive_more::{Display, Error}; #[cfg(feature = "full")] use redis::RedisError; +#[cfg(feature = "full")] +use tokio::sync::oneshot::error::RecvError; /// Error datatype #[derive(Debug, PartialEq, Display, Error)] @@ -88,6 +90,11 @@ pub enum CaptchaError { #[cfg(feature = "full")] RedisError(RedisError), + /// Channel receive error + #[display(fmt = "{}", _0)] + #[cfg(feature = "full")] + RecvError(RecvError), + /// Weird behaviour from mcaptcha redis module #[display( fmt = "Something weird happening with mCaptcha redis module. Please file bug report" @@ -121,6 +128,15 @@ impl From for CaptchaError { } } +#[cfg(feature = "full")] +#[cfg(not(tarpaulin_include))] +impl From for CaptchaError { + fn from(e: RecvError) -> Self { + log::error!("{:?}", e); + Self::RecvError(e) + } +} + #[cfg(feature = "full")] #[cfg(not(tarpaulin_include))] impl From for CaptchaError { diff --git a/src/lib.rs b/src/lib.rs index f232f36..f82f152 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -138,7 +138,7 @@ //! //! // Get PoW config. Should be called everytime there's a visitor for a //! // managed site(here mcaptcha_name) -//! let work_req = system.get_pow(mcaptcha_name.into()).await.unwrap(); +//! let work_req = system.get_pow(mcaptcha_name.into()).await.unwrap().unwrap(); //! //! // the following computation should be done on the client but for the purpose //! // of this illustration, we are going to do it on the server it self diff --git a/src/system.rs b/src/system.rs index d892c6d..48d5f25 100644 --- a/src/system.rs +++ b/src/system.rs @@ -85,15 +85,8 @@ where ::Context: ToEnvelope + ToEnvelope, { /// utility function to get difficulty factor of site `id` and cache it - pub async fn get_pow(&self, id: String) -> Option { - match self - .master - .send(AddVisitor(id.clone())) - .await - .unwrap() - .await - .unwrap() - { + pub async fn get_pow(&self, id: String) -> CaptchaResult> { + match self.master.send(AddVisitor(id.clone())).await?.await? { Ok(Some(mcaptcha)) => { let pow_config = PoWConfig::new(mcaptcha.difficulty_factor, self.pow.salt.clone()); @@ -105,16 +98,10 @@ where .build() .unwrap(); - self.cache - .send(cache_msg) - .await - .unwrap() - .await - .unwrap() - .unwrap(); - Some(pow_config) + self.cache.send(cache_msg).await?.await??; + Ok(Some(pow_config)) } - _ => None, + _ => Ok(None), } } @@ -127,7 +114,7 @@ where }; let msg = RetrivePoW(msg); - let cached_config = self.cache.send(msg).await.unwrap().await.unwrap()?; + let cached_config = self.cache.send(msg).await?.await??; if cached_config.is_none() { return Err(CaptchaError::StringNotFound); @@ -154,7 +141,7 @@ where let msg: CacheResult = cached_config.into(); let res = msg.token.clone(); - self.cache.send(msg).await.unwrap().await.unwrap()?; + self.cache.send(msg).await?.await??; Ok(res) } @@ -163,7 +150,7 @@ where &self, msg: VerifyCaptchaResult, ) -> CaptchaResult { - self.cache.send(msg).await.unwrap().await.unwrap() + self.cache.send(msg).await?.await? } } @@ -211,7 +198,7 @@ mod tests { #[actix_rt::test] async fn get_pow_works() { let actors = boostrap_system(10).await; - let pow = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap(); + let pow = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap().unwrap(); assert_eq!(pow.difficulty_factor, LEVEL_1.0); } @@ -220,7 +207,7 @@ mod tests { // start system let actors = boostrap_system(10).await; // get work - let work_req = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap(); + let work_req = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap().unwrap(); // get config let config = get_config(); @@ -261,7 +248,7 @@ mod tests { let res = actors.verify_pow(payload.clone()).await; assert_eq!(res, Err(CaptchaError::StringNotFound)); - let insufficient_work_req = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap(); + let insufficient_work_req = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap().unwrap(); let insufficient_work = config.prove_work(&insufficient_work_req.string, 1).unwrap(); let insufficient_work_payload = Work { string: insufficient_work_req.string, @@ -272,7 +259,7 @@ mod tests { let res = actors.verify_pow(insufficient_work_payload.clone()).await; assert_eq!(res, Err(CaptchaError::InsuffiencientDifficulty)); - let sitekeyfail_config = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap(); + let sitekeyfail_config = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap().unwrap(); let sitekeyfail_work = config .prove_work( &sitekeyfail_config.string,