Skip to content

Commit 5c2bc45

Browse files
committed
give structured context for registry fetch failure
1 parent dd6fcda commit 5c2bc45

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/crates/registry.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::CrateTrait;
22
use crate::{PrepareError, Workspace};
3-
use anyhow::Context as _;
3+
use anyhow::{anyhow, Context as _};
44
use flate2::read::GzDecoder;
55
use log::info;
66
use std::fs::File;
@@ -163,12 +163,22 @@ impl CrateTrait for RegistryCrate {
163163
std::fs::create_dir_all(parent)?;
164164
}
165165

166-
workspace
166+
match workspace
167167
.http_client()
168168
.get(self.fetch_url(workspace)?)
169169
.send()?
170-
.error_for_status()?
171-
.write_to(&mut BufWriter::new(File::create(&local)?))?;
170+
.error_for_status()
171+
{
172+
Ok(resonse) => resonse,
173+
Err(err) => {
174+
return Err(anyhow!(err).context(PrepareError::RegistryOperationFailed {
175+
action: "fetch",
176+
krate: self.name.clone(),
177+
version: self.version.clone(),
178+
}))
179+
}
180+
}
181+
.write_to(&mut BufWriter::new(File::create(&local)?))?;
172182

173183
Ok(())
174184
}
@@ -194,7 +204,8 @@ impl CrateTrait for RegistryCrate {
194204
);
195205
if let Err(err) = unpack_without_first_dir(&mut tar, dest) {
196206
let _ = crate::utils::remove_dir_all(dest);
197-
Err(err.context(PrepareError::UnpackFailed {
207+
Err(err.context(PrepareError::RegistryOperationFailed {
208+
action: "unpack",
198209
krate: self.name.clone(),
199210
version: self.version.clone(),
200211
}))

src/prepare.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,14 @@ pub enum PrepareError {
424424
/// repo url
425425
url: String,
426426
},
427-
/// failed to unpack crate
428-
#[error("failed to unpack {krate} version {version}")]
429-
UnpackFailed {
430-
/// the crates name
427+
/// failed to fetch/unpack a crate from a registry
428+
#[error("failed to {action} {krate} version {version}")]
429+
RegistryOperationFailed {
430+
/// the action that failed
431+
action: &'static str,
432+
/// the name of the crate
431433
krate: String,
432-
/// the crates version
434+
/// the version of the crate
433435
version: String,
434436
},
435437
}

0 commit comments

Comments
 (0)