Skip to content

Commit

Permalink
Make source of IO errors more obvious
Browse files Browse the repository at this point in the history
  • Loading branch information
cole-h committed Jun 6, 2024
1 parent 06ffb16 commit 0a64d2c
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions magic-nix-cache/src/flakehub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,24 @@ pub async fn init_cache(
) -> Result<State> {
// Parse netrc to get the credentials for api.flakehub.com.
let netrc = {
let mut netrc_file = File::open(flakehub_api_server_netrc).await?;
let mut netrc_file = File::open(flakehub_api_server_netrc).await.map_err(|e| {
Error::Internal(format!(
"Failed to open {}: {}",
flakehub_api_server_netrc.display(),
e
))
})?;
let mut netrc_contents = String::new();
netrc_file.read_to_string(&mut netrc_contents).await?;
netrc_file
.read_to_string(&mut netrc_contents)
.await
.map_err(|e| {
Error::Internal(format!(
"Failed to read {} contents: {}",
flakehub_api_server_netrc.display(),
e
))
})?;
netrc_rs::Netrc::parse(netrc_contents, false).map_err(Error::Netrc)?
};

Expand Down Expand Up @@ -84,7 +99,15 @@ pub async fn init_cache(
.create(false)
.append(true)
.open(flakehub_api_server_netrc)
.await?;
.await
.map_err(|e| {
Error::Internal(format!(
"Failed to open {} for appending: {}",
flakehub_api_server_netrc.display(),
e
))
})?;

netrc_file
.write_all(
format!(
Expand All @@ -93,7 +116,14 @@ pub async fn init_cache(
)
.as_bytes(),
)
.await?;
.await
.map_err(|e| {
Error::Internal(format!(
"Failed to write credentials to {}: {}",
flakehub_api_server_netrc.display(),
e
))
})?;
}

let server_config = ServerConfig {
Expand Down

0 comments on commit 0a64d2c

Please sign in to comment.