Convert the Io error type to have a string of context

This commit is contained in:
Graham Christensen 2024-08-29 16:37:49 -04:00
parent a4866b9dcd
commit edb35e8b94
3 changed files with 16 additions and 5 deletions

View file

@ -112,7 +112,8 @@ async fn workflow_finish(
// NOTE(cole-h): see `init_logging` // NOTE(cole-h): see `init_logging`
if let Some(logfile) = &state.logfile { if let Some(logfile) = &state.logfile {
let logfile_contents = std::fs::read_to_string(logfile)?; let logfile_contents = std::fs::read_to_string(logfile)
.map_err(|e| crate::error::Error::Io(e, format!("Reading {}", logfile.display())))?;
println!("Every log line throughout the lifetime of the program:"); println!("Every log line throughout the lifetime of the program:");
println!("\n{logfile_contents}\n"); println!("\n{logfile_contents}\n");
} }

View file

@ -19,8 +19,8 @@ pub enum Error {
#[error("Bad Request")] #[error("Bad Request")]
BadRequest, BadRequest,
#[error("I/O error: {0}")] #[error("I/O error: {0}. Context: {1}")]
Io(#[from] std::io::Error), Io(std::io::Error, String),
#[error("GHA cache is disabled")] #[error("GHA cache is disabled")]
GHADisabled, GHADisabled,

View file

@ -11,9 +11,19 @@ use crate::error::Result;
pub async fn get_store_paths(store: &NixStore) -> Result<HashSet<PathBuf>> { pub async fn get_store_paths(store: &NixStore) -> Result<HashSet<PathBuf>> {
// FIXME: use the Nix API. // FIXME: use the Nix API.
let store_dir = store.store_dir(); let store_dir = store.store_dir();
let mut listing = tokio::fs::read_dir(store_dir).await?; let mut listing = tokio::fs::read_dir(store_dir).await.map_err(|e| {
crate::error::Error::Io(
e,
format!("Enumerating store paths in {}", store_dir.display()),
)
})?;
let mut paths = HashSet::new(); let mut paths = HashSet::new();
while let Some(entry) = listing.next_entry().await? { while let Some(entry) = listing.next_entry().await.map_err(|e| {
crate::error::Error::Io(
e,
format!("Reading existing store paths from {}", store_dir.display()),
)
})? {
let file_name = entry.file_name(); let file_name = entry.file_name();
let file_name = Path::new(&file_name); let file_name = Path::new(&file_name);