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
1 changed files with 33 additions and 3 deletions

View File

@ -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)]