From edb35e8b9449e97ef819d514643b46b8e1875593 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Thu, 29 Aug 2024 16:37:49 -0400 Subject: [PATCH] Convert the Io error type to have a string of context --- magic-nix-cache/src/api.rs | 3 ++- magic-nix-cache/src/error.rs | 4 ++-- magic-nix-cache/src/util.rs | 14 ++++++++++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/magic-nix-cache/src/api.rs b/magic-nix-cache/src/api.rs index 7aa6c63..104d7f1 100644 --- a/magic-nix-cache/src/api.rs +++ b/magic-nix-cache/src/api.rs @@ -112,7 +112,8 @@ async fn workflow_finish( // NOTE(cole-h): see `init_logging` 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!("\n{logfile_contents}\n"); } diff --git a/magic-nix-cache/src/error.rs b/magic-nix-cache/src/error.rs index ec1b8d3..d975616 100644 --- a/magic-nix-cache/src/error.rs +++ b/magic-nix-cache/src/error.rs @@ -19,8 +19,8 @@ pub enum Error { #[error("Bad Request")] BadRequest, - #[error("I/O error: {0}")] - Io(#[from] std::io::Error), + #[error("I/O error: {0}. Context: {1}")] + Io(std::io::Error, String), #[error("GHA cache is disabled")] GHADisabled, diff --git a/magic-nix-cache/src/util.rs b/magic-nix-cache/src/util.rs index 2c7e759..db88ffb 100644 --- a/magic-nix-cache/src/util.rs +++ b/magic-nix-cache/src/util.rs @@ -11,9 +11,19 @@ use crate::error::Result; pub async fn get_store_paths(store: &NixStore) -> Result> { // FIXME: use the Nix API. 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(); - 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 = Path::new(&file_name);