Skip to content

Commit 911dd55

Browse files
committed
Applied suggestions for #1043
Tests are still TODO
1 parent 5c62d58 commit 911dd55

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

src/web/mod.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ struct MatchVersion {
247247
impl MatchVersion {
248248
/// If the matched version was an exact match to the requested crate name, returns the
249249
/// `MatchSemver` for the query. If the lookup required a dash/underscore conversion, returns
250-
/// `Nope::CrateNotFound`.
250+
/// `CrateNotFound`.
251251
fn assume_exact(self) -> Result<MatchSemver, Nope> {
252252
if self.corrected_name.is_none() {
253253
Ok(self.version)
@@ -339,7 +339,7 @@ fn match_version(
339339
}
340340

341341
// Now try to match with semver
342-
let req_sem_ver = VersionReq::parse(&req_version).map_err(|_| Nope::CrateNotFound)?;
342+
let req_sem_ver = VersionReq::parse(&req_version).map_err(|_| Nope::VersionNotFound)?;
343343

344344
// we need to sort versions first
345345
let versions_sem = {
@@ -348,7 +348,14 @@ fn match_version(
348348
for version in versions.iter().filter(|(_, _, yanked)| !yanked) {
349349
// in theory a crate must always have a semver compatible version, but check result just in case
350350
versions_sem.push((
351-
Version::parse(&version.0).map_err(|_| Nope::InternalServerError)?,
351+
Version::parse(&version.0).map_err(|_| {
352+
log::error!(
353+
"invalid semver in database for crate {}: {}",
354+
name,
355+
version.0
356+
);
357+
Nope::InternalServerError
358+
})?,
352359
version.1,
353360
));
354361
}
@@ -377,7 +384,7 @@ fn match_version(
377384
corrected_name,
378385
version: MatchSemver::Semver((v.0.to_string(), v.1)),
379386
})
380-
.ok_or(Nope::CrateNotFound);
387+
.ok_or(Nope::VersionNotFound);
381388
}
382389

383390
// Since we return with a CrateNotFound earlier if the db reply is empty,
@@ -598,14 +605,13 @@ mod test {
598605
}
599606

600607
fn version(v: Option<&str>, db: &TestDatabase) -> Option<String> {
601-
match_version(&mut db.conn(), "foo", v)
602-
.ok()
603-
.and_then(|version| {
604-
version
605-
.assume_exact()
606-
.ok()
607-
.map(|semver| semver.into_parts().0)
608-
})
608+
let version = match_version(&mut db.conn(), "foo", v)
609+
.ok()?
610+
.assume_exact()
611+
.ok()?
612+
.into_parts()
613+
.0;
614+
Some(version)
609615
}
610616

611617
fn semver(version: &'static str) -> Option<String> {

src/web/rustdoc.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -282,33 +282,31 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
282282
rendering_time.step("match version");
283283

284284
// Check the database for releases with the requested version while doing the following:
285+
// * If no matching releases are found, return a 404 with the underlying error
286+
// Then:
285287
// * If both the name and the version are an exact match, return the version of the crate.
286288
// * If there is an exact match, but the requested crate name was corrected (dashes vs. underscores), redirect to the corrected name.
287289
// * If there is a semver (but not exact) match, redirect to the exact version.
288-
// * Otherwise, return a 404.
289-
let version = match match_version(&mut conn, &name, url_version) {
290-
Ok(match_vers) => {
291-
match match_vers.version {
292-
MatchSemver::Exact((version, _)) => {
293-
// Redirect when the requested crate name isn't correct
294-
if let Some(name) = match_vers.corrected_name {
295-
return redirect(&name, &version, &req_path);
296-
}
290+
let release_found = match_version(&mut conn, &name, url_version)
291+
.map_err(|err| IronError::new(err, status::NotFound))?;
292+
293+
let version = match release_found.version {
294+
MatchSemver::Exact((version, _)) => {
295+
// Redirect when the requested crate name isn't correct
296+
if let Some(name) = release_found.corrected_name {
297+
return redirect(&name, &version, &req_path);
298+
}
297299

298-
version
299-
}
300+
version
301+
}
300302

301-
// Redirect when the requested version isn't correct
302-
MatchSemver::Semver((v, _)) => {
303-
// to prevent cloudfront caching the wrong artifacts on URLs with loose semver
304-
// versions, redirect the browser to the returned version instead of loading it
305-
// immediately
306-
return redirect(&name, &v, &req_path);
307-
}
308-
}
303+
// Redirect when the requested version isn't correct
304+
MatchSemver::Semver((v, _)) => {
305+
// to prevent cloudfront caching the wrong artifacts on URLs with loose semver
306+
// versions, redirect the browser to the returned version instead of loading it
307+
// immediately
308+
return redirect(&name, &v, &req_path);
309309
}
310-
// Return a 404, as a crate by that name and version requirement doesn't exist
311-
Err(err) => return Err(IronError::new(err, status::NotFound)),
312310
};
313311

314312
rendering_time.step("crate details");
@@ -563,6 +561,12 @@ pub fn badge_handler(req: &mut Request) -> IronResult<Response> {
563561
return Ok(super::redirect(iron_url));
564562
}
565563

564+
Err(Nope::VersionNotFound) => BadgeOptions {
565+
subject: "docs".to_owned(),
566+
status: "version not found".to_owned(),
567+
color: "#e05d44".to_owned(),
568+
},
569+
566570
Err(_) => BadgeOptions {
567571
subject: "docs".to_owned(),
568572
status: "no builds".to_owned(),

0 commit comments

Comments
 (0)