From c477a92945bf3466b303e94d867b0251a466349c Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sun, 5 Mar 2023 11:15:32 -0500 Subject: [PATCH] Added initialization failure. --- src/morethantext/cache.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/morethantext/cache.rs b/src/morethantext/cache.rs index 7b24767..184811f 100644 --- a/src/morethantext/cache.rs +++ b/src/morethantext/cache.rs @@ -184,15 +184,21 @@ impl Entry { struct Cache; impl Cache { - async fn new

(dir: P) -> Self + async fn new

(dir: P) -> Result where P: Into, { let pathbuf = dir.into(); let entry = pathbuf.as_path().join(ENTRY); let store = DataType::new("store").unwrap(); - Entry::new(entry, store).await.unwrap(); - Self {} + match Entry::new(entry, store).await { + Ok(_) => Ok(Self {}), + Err(err) => { + let mut error = DBError::new("initialization failure"); + error.add_source(err); + Err(error) + } + } } } @@ -580,12 +586,13 @@ mod entry { #[cfg(test)] mod cache { use super::*; + use std::error::Error; use tempfile::tempdir; #[async_std::test] async fn create() { let dir = tempdir().unwrap(); - Cache::new(dir.path().to_str().unwrap()).await; + Cache::new(dir.path().to_str().unwrap()).await.unwrap(); let epoint = dir.path().join(ENTRY); assert!( epoint.is_file(), @@ -598,4 +605,25 @@ mod cache { Vec::::new() ); } + + #[async_std::test] + async fn entry_failure() { + let dir = tempdir().unwrap(); + let path = dir.path().join("bad").join("path"); + match Cache::new(path).await { + Ok(_) => assert!(false, "Should have produced an error."), + Err(err) => { + assert_eq!(err.to_string(), "initialization failure"); + assert!(err.source().is_some(), "Error should have a source."); + assert!( + err.source() + .unwrap() + .to_string() + .contains("failed to write"), + "Source Error Message: {}", + err.source().unwrap().to_string() + ); + } + } + } }