From d73a18be2d5a3ee218cdfc8d792f7efd859a368a Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Tue, 6 Dec 2022 11:42:11 -0500 Subject: [PATCH] Added error for missing cache entry. --- src/morethantext/mod.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 9a9c253..5281288 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -54,9 +54,12 @@ impl MoreThanText { return id; } - async fn get_entry(&self, id: &str) -> CacheEntry { + async fn get_entry(&self, id: &str) -> Result { let cache = self.cache.lock().await; - cache.get(id).unwrap().clone() + match cache.get(id) { + Some(id) => Ok(id.clone()), + None => Err(DBError::new("cache entry not found")), + } } } @@ -142,7 +145,16 @@ mod cache { let data = "something"; let expected = CacheEntry::Raw(data.to_string()); let id = mtt.db.add_entry(expected).await; - let output = mtt.db.get_entry(&id).await; + let output = mtt.db.get_entry(&id).await.unwrap(); assert_eq!(output.to_string(), data); } + + #[async_std::test] + async fn retrieve_bad_id() { + let mtt = MTT::new().await; + match mtt.db.get_entry(&"Not Valid").await { + Ok(_) => assert!(false, "Should have raised an error."), + Err(err) => assert_eq!(err.to_string(), "cache entry not found"), + } + } }