Added file errors to update entry.

This commit is contained in:
Jeff Baskin 2022-12-19 10:33:04 -05:00
parent 96ed474568
commit 1403aaa935

View File

@ -99,9 +99,14 @@ impl MoreThanText {
Ok(_) => (), Ok(_) => (),
Err(err) => return Err(err), Err(err) => return Err(err),
} }
write(Path::new(&self.dir).join(&id), entry.to_bytes()) match write(Path::new(&self.dir).join(&id), entry.to_bytes()).await {
.await Ok(_) => (),
.unwrap(); Err(err) => {
let mut error = DBError::new("data write");
error.add_source(err);
return Err(error);
}
}
let mut cache = self.cache.lock().await; let mut cache = self.cache.lock().await;
cache.insert(id.to_string(), entry); cache.insert(id.to_string(), entry);
Ok(()) Ok(())
@ -262,6 +267,31 @@ mod cache {
Err(err) => assert_eq!(err.to_string(), "cache entry not found"), 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)] #[cfg(test)]