From 90ab5809b1d8c00f294bab35374d88dbeaa39c96 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Tue, 18 Apr 2023 09:02:18 -0400 Subject: [PATCH] Improved query error handling. --- src/morethantext/mod.rs | 69 +++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 80df136..08588a3 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -9,15 +9,17 @@ const ENTRY: &str = "EntryPoint"; #[derive(Debug)] enum ErrorCode { + // General Undefined(String), - IncorrectDataType(String), + // Cache Entry + EntryNotFound(String), } impl fmt::Display for ErrorCode { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ErrorCode::Undefined(msg) => write!(f, "{}", msg), - ErrorCode::IncorrectDataType(item) => write!(f, "data type '{}' does not exist", item), + ErrorCode::EntryNotFound(id) => write!(f, "entry '{}' was not found", id), } } } @@ -36,13 +38,10 @@ mod errorcodes { } #[test] - fn incorrect_data_type() { + fn bad_entry() { for item in ITEMS { - let err = ErrorCode::IncorrectDataType(item.to_string()); - assert_eq!( - err.to_string(), - format!("data type '{}' does not exist", item) - ); + let err = ErrorCode::EntryNotFound(item.to_string()); + assert_eq!(err.to_string(), format!("entry '{}' was not found", item)); } } } @@ -107,12 +106,12 @@ mod errors { } #[test] - fn create_from_different_code() { - let code = ErrorCode::IncorrectDataType("nuts".to_string()); + fn create_missing_entry() { + let code = ErrorCode::EntryNotFound("an_id".to_string()); let err = MTTError::from_code(code); match err.code { - ErrorCode::IncorrectDataType(_) => (), - _ => assert!(false, "{:?} is not incorrect data type", err.code), + ErrorCode::EntryNotFound(_) => (), + _ => assert!(false, "{:?} is not undefined", err.code), } } } @@ -274,7 +273,9 @@ impl Cache { if id == ENTRY { output.insert(ENTRY.to_string(), DataType::new("store")); } else { - return Err(MTTError::new("fred")); + return Err(MTTError::from_code(ErrorCode::EntryNotFound( + id.to_string(), + ))); } } Ok(output) @@ -287,17 +288,13 @@ impl Cache { async fn start(&self, listener: Receiver) { loop { match listener.recv().await.unwrap() { - ToCache::Query(qry) => { - match self.query(&qry.ids).await { - Ok(data) => qry.reply.send(FromCache::Data(data)).await.unwrap(), - Err(error) => qry.reply.send(FromCache::Error(error)).await.unwrap(), - } + ToCache::Query(qry) => match self.query(&qry.ids).await { + Ok(data) => qry.reply.send(FromCache::Data(data)).await.unwrap(), + Err(error) => qry.reply.send(FromCache::Error(error)).await.unwrap(), }, - ToCache::Commit(commit) => { - match self.commit().await { - Ok(_) => commit.reply.send(FromCache::Ok).await.unwrap(), - Err(error) => commit.reply.send(FromCache::Error(error)).await.unwrap(), - } + ToCache::Commit(commit) => match self.commit().await { + Ok(_) => commit.reply.send(FromCache::Ok).await.unwrap(), + Err(error) => commit.reply.send(FromCache::Error(error)).await.unwrap(), }, } } @@ -376,9 +373,29 @@ mod caches { } #[async_std::test] - async fn add_database_commit() { - //let dir = tempdir().unwrap(); - //let s_cache = start_cache(dir.path()).await; + async fn get_store() { + let dir = tempdir().unwrap(); + let cache = Cache::new(dir.path()).await.unwrap(); + let output = cache.query(&[ENTRY.to_string()].to_vec()).await.unwrap(); + let result = output.get(ENTRY).unwrap(); + match result { + DataType::DBMap(_) => (), + _ => assert!(false, "{:?} should have been an Ok.", result), + } + } + + #[async_std::test] + async fn bad_get() { + let dir = tempdir().unwrap(); + let cache = Cache::new(dir.path()).await.unwrap(); + let bad_id = "really_bad_id"; + match cache.query(&[bad_id.to_string()].to_vec()).await { + Ok(_) => assert!(false, "Should have produced an error."), + Err(err) => match err.code { + ErrorCode::EntryNotFound(id) => assert_eq!(id, bad_id), + _ => assert!(false, "{:?} should have been EntryNotFound.", err.code), + }, + } } }