Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: graceful error handling for grit check in uninitialized contexts #61

Merged
merged 2 commits into from
Mar 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions crates/gritcache/src/new_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ impl ThreadedCache {
};

let (sender, receiver) = mpsc::channel::<HashKey>();
let mut writer = Self::new_writer(&mismatches_path)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@morgante let me know if there's a reason not to do this i'm missing - seems more straightforward to bail during creation than to do so from the writer thread

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine - if the compiler is happy, I'm happy.

let manager = thread::spawn(move || {
let mut writer = Self::new_writer(&mismatches_path).unwrap();
while let Ok(key) = receiver.recv() {
writer.write_all(&key).unwrap();
}
Expand Down Expand Up @@ -105,7 +105,8 @@ impl ThreadedCache {
.create(true)
.append(true)
.open(path)
.context("Failed to open cache file".to_string())?,
.context("Failed to open cache file".to_string())
.context("Please run `grit init` or set GRIT_CACHE_DIR to cache check results")?,
);

Ok(writer)
Expand Down Expand Up @@ -172,6 +173,7 @@ mod tests {

let path = PathBuf::from(".");
let mismatches_cache_path = path.join(MISMATCHES_CACHE_NAME);
let bad_path = PathBuf::from("./doesnotexist").join(MISMATCHES_CACHE_NAME);

println!(
"mismatches_cache_path: {}",
Expand All @@ -183,6 +185,9 @@ mod tests {
std::fs::remove_file(&mismatches_cache_path)?;
}

// assert cache creation fails gracefully on invalid paths
assert!(ThreadedCache::new(bad_path.clone(), false).await.is_err());

// Create an empty cache
let (cache, manager) = ThreadedCache::new(path.clone(), true).await?;

Expand Down
Loading