Skip to content

Commit 7ecdca9

Browse files
authored
Merge pull request #932 from epage/error
fix(upgrade): Provide more context on index failures
2 parents cfb66ca + 9bb832e commit 7ecdca9

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

src/bin/upgrade/upgrade.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,7 @@ fn exec(args: UpgradeArgs) -> CargoResult<()> {
289289
// Update indices for any alternative registries, unless
290290
// we're offline.
291291
let registry_url = registry_url(&manifest_path, dependency.registry())?;
292-
let index = index.index(&registry_url)?;
293-
let krate = index.krate(&dependency.name)?;
292+
let krate = index.krate(&registry_url, &dependency.name)?;
294293
let versions = krate
295294
.as_ref()
296295
.map(|k| k.versions.as_slice())

src/index.rs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ impl IndexCache {
3131
/// Determines if the specified crate exists in the crates.io index
3232
#[inline]
3333
pub fn has_krate(&mut self, registry: &Url, name: &str) -> CargoResult<bool> {
34-
self.index(registry)?.has_krate(name)
34+
self.index(registry)
35+
.with_context(|| format!("failed to look up {name}"))?
36+
.has_krate(name)
3537
}
3638

3739
/// Determines if the specified crate version exists in the crates.io index
@@ -42,20 +44,26 @@ impl IndexCache {
4244
name: &str,
4345
version: &str,
4446
) -> CargoResult<Option<bool>> {
45-
self.index(registry)?.has_krate_version(name, version)
47+
self.index(registry)
48+
.with_context(|| format!("failed to look up {name}@{version}"))?
49+
.has_krate_version(name, version)
4650
}
4751

4852
#[inline]
4953
pub fn update_krate(&mut self, registry: &Url, name: &str) -> CargoResult<()> {
50-
self.index(registry)?.update_krate(name);
54+
self.index(registry)
55+
.with_context(|| format!("failed to look up {name}"))?
56+
.update_krate(name);
5157
Ok(())
5258
}
5359

5460
pub fn krate(&mut self, registry: &Url, name: &str) -> CargoResult<Option<IndexKrate>> {
55-
self.index(registry)?.krate(name)
61+
self.index(registry)
62+
.with_context(|| format!("failed to look up {name}"))?
63+
.krate(name)
5664
}
5765

58-
pub fn index<'s>(&'s mut self, registry: &Url) -> CargoResult<&'s mut AnyIndexCache> {
66+
fn index<'s>(&'s mut self, registry: &Url) -> CargoResult<&'s mut AnyIndexCache> {
5967
if !self.index.contains_key(registry) {
6068
let index = AnyIndex::open(registry, self.certs_source)?;
6169
let index = AnyIndexCache::new(index);
@@ -65,14 +73,14 @@ impl IndexCache {
6573
}
6674
}
6775

68-
pub struct AnyIndexCache {
76+
struct AnyIndexCache {
6977
index: AnyIndex,
7078
cache: std::collections::HashMap<String, Option<IndexKrate>>,
7179
}
7280

7381
impl AnyIndexCache {
7482
#[inline]
75-
pub fn new(index: AnyIndex) -> Self {
83+
fn new(index: AnyIndex) -> Self {
7684
Self {
7785
index,
7886
cache: std::collections::HashMap::new(),
@@ -81,23 +89,23 @@ impl AnyIndexCache {
8189

8290
/// Determines if the specified crate exists in the crates.io index
8391
#[inline]
84-
pub fn has_krate(&mut self, name: &str) -> CargoResult<bool> {
92+
fn has_krate(&mut self, name: &str) -> CargoResult<bool> {
8593
Ok(self.krate(name)?.map(|_| true).unwrap_or(false))
8694
}
8795

8896
/// Determines if the specified crate version exists in the crates.io index
8997
#[inline]
90-
pub fn has_krate_version(&mut self, name: &str, version: &str) -> CargoResult<Option<bool>> {
98+
fn has_krate_version(&mut self, name: &str, version: &str) -> CargoResult<Option<bool>> {
9199
let krate = self.krate(name)?;
92100
Ok(krate.map(|ik| ik.versions.iter().any(|iv| iv.version == version)))
93101
}
94102

95103
#[inline]
96-
pub fn update_krate(&mut self, name: &str) {
104+
fn update_krate(&mut self, name: &str) {
97105
self.cache.remove(name);
98106
}
99107

100-
pub fn krate(&mut self, name: &str) -> CargoResult<Option<IndexKrate>> {
108+
fn krate(&mut self, name: &str) -> CargoResult<Option<IndexKrate>> {
101109
if let Some(entry) = self.cache.get(name) {
102110
return Ok(entry.clone());
103111
}
@@ -108,45 +116,49 @@ impl AnyIndexCache {
108116
}
109117
}
110118

111-
pub enum AnyIndex {
119+
enum AnyIndex {
112120
Local(LocalIndex),
113121
Remote(RemoteIndex),
114122
}
115123

116124
impl AnyIndex {
117-
pub fn open(url: &Url, certs_source: CertsSource) -> CargoResult<Self> {
125+
fn open(url: &Url, certs_source: CertsSource) -> CargoResult<Self> {
118126
if url.scheme() == "file" {
119-
LocalIndex::open(url).map(Self::Local)
127+
LocalIndex::open(url)
128+
.map(Self::Local)
129+
.with_context(|| format!("invalid local registry {url:?}"))
120130
} else {
121-
RemoteIndex::open(url, certs_source).map(Self::Remote)
131+
RemoteIndex::open(url, certs_source)
132+
.map(Self::Remote)
133+
.with_context(|| format!("invalid registry {url:?}"))
122134
}
123135
}
124136

125-
pub(crate) fn krate(&mut self, name: &str) -> CargoResult<Option<IndexKrate>> {
137+
fn krate(&mut self, name: &str) -> CargoResult<Option<IndexKrate>> {
126138
match self {
127139
Self::Local(index) => index.krate(name),
128140
Self::Remote(index) => index.krate(name),
129141
}
130142
}
131143
}
132144

133-
pub struct LocalIndex {
145+
struct LocalIndex {
134146
index: tame_index::index::LocalRegistry,
135147
root: tame_index::PathBuf,
136148
}
137149

138150
impl LocalIndex {
139-
pub fn open(url: &Url) -> CargoResult<Self> {
151+
fn open(url: &Url) -> CargoResult<Self> {
140152
let path = url
141153
.to_file_path()
142-
.map_err(|()| anyhow::format_err!("invalid local registry {url}"))?;
154+
.map_err(|_err| anyhow::format_err!("invalid file path {url:?}"))?;
143155
let path = tame_index::PathBuf::from_path_buf(path)
144-
.map_err(|_err| anyhow::format_err!("invalid local registry {url:?}"))?;
156+
.map_err(|_err| anyhow::format_err!("invalid file path {url:?}"))?;
145157
let index = tame_index::index::LocalRegistry::open(path.clone(), false)?;
146158
Ok(Self { index, root: path })
147159
}
148160

149-
pub(crate) fn krate(&mut self, name: &str) -> CargoResult<Option<IndexKrate>> {
161+
fn krate(&mut self, name: &str) -> CargoResult<Option<IndexKrate>> {
150162
let name = tame_index::KrateName::cargo(name)?;
151163
// HACK: for some reason, `tame_index` puts `index` in the middle
152164
let entry_path = self.index.krate_path(name);
@@ -165,15 +177,15 @@ impl LocalIndex {
165177
}
166178
}
167179

168-
pub struct RemoteIndex {
180+
struct RemoteIndex {
169181
index: tame_index::SparseIndex,
170182
client: tame_index::external::reqwest::blocking::Client,
171183
lock: FileLock,
172184
etags: Vec<(String, String)>,
173185
}
174186

175187
impl RemoteIndex {
176-
pub fn open(url: &Url, certs_source: CertsSource) -> CargoResult<Self> {
188+
fn open(url: &Url, certs_source: CertsSource) -> CargoResult<Self> {
177189
let url = url.to_string();
178190
let url = tame_index::IndexUrl::NonCratesIo(std::borrow::Cow::Owned(url));
179191
let index = tame_index::SparseIndex::new(tame_index::IndexLocation::new(url))?;
@@ -199,7 +211,7 @@ impl RemoteIndex {
199211
})
200212
}
201213

202-
pub(crate) fn krate(&mut self, name: &str) -> CargoResult<Option<IndexKrate>> {
214+
fn krate(&mut self, name: &str) -> CargoResult<Option<IndexKrate>> {
203215
let etag = self
204216
.etags
205217
.iter()

0 commit comments

Comments
 (0)