From 1403aaa93574065ae22ca01950ea21bffee1ed4a Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Mon, 19 Dec 2022 10:33:04 -0500 Subject: [PATCH] Added file errors to update entry. --- src/morethantext/mod.rs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 1f21fe2..9e797f7 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -99,9 +99,14 @@ impl MoreThanText { Ok(_) => (), Err(err) => return Err(err), } - write(Path::new(&self.dir).join(&id), entry.to_bytes()) - .await - .unwrap(); + match write(Path::new(&self.dir).join(&id), entry.to_bytes()).await { + Ok(_) => (), + Err(err) => { + let mut error = DBError::new("data write"); + error.add_source(err); + return Err(error); + } + } let mut cache = self.cache.lock().await; cache.insert(id.to_string(), entry); Ok(()) @@ -262,6 +267,31 @@ mod cache { Err(err) => assert_eq!(err.to_string(), "cache entry not found"), } } + + #[async_std::test] + async fn update_bad_file() { + let mtt = MTT::new().await; + let msg = "could not write to file"; + let id = mtt + .db + .add_entry(CacheEntry::Raw("fleeting".to_string())) + .await + .unwrap(); + mtt.create_io_error().await; + match mtt + .db + .update_entry(&id, CacheEntry::Raw("failure".to_string())) + .await + { + Ok(_) => assert!(false, "This should produce a write failure."), + Err(err) => { + assert_eq!(err.to_string(), "data write"); + assert!(err.source().is_some(), "Must include the source error."); + let err_msg = err.source().unwrap().to_string(); + assert!(err_msg.contains(msg), "'{}' not in '{}'", msg, err_msg); + } + } + } } #[cfg(test)]