Skip to content

Commit

Permalink
Show link we failed on parsing index pages (#9118)
Browse files Browse the repository at this point in the history
For #8172, show the link we failed on. This should e.g. give a hint on
permission denied pages such as
#8172 (comment).
  • Loading branch information
konstin authored Nov 14, 2024
1 parent b37170d commit 57ff533
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions crates/uv-client/src/html.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str::FromStr;

use tl::HTMLTag;
use tl::{HTMLTag, Parser};
use tracing::{instrument, warn};
use url::Url;

Expand Down Expand Up @@ -44,7 +44,7 @@ impl SimpleHtml {
.iter()
.filter_map(|node| node.as_tag())
.filter(|link| link.name().as_bytes() == b"a")
.map(|link| Self::parse_anchor(link))
.map(|link| Self::parse_anchor(link, dom.parser()))
.collect::<Result<Vec<_>, _>>()?;
// While it has not been positively observed, we sort the files
// to ensure we have a defined ordering. Otherwise, if we rely on
Expand All @@ -70,14 +70,14 @@ impl SimpleHtml {
}

/// Parse a [`File`] from an `<a>` tag.
fn parse_anchor(link: &HTMLTag) -> Result<File, Error> {
fn parse_anchor(link: &HTMLTag, parser: &Parser) -> Result<File, Error> {
// Extract the href.
let href = link
.attributes()
.get("href")
.flatten()
.filter(|bytes| !bytes.as_bytes().is_empty())
.ok_or(Error::MissingHref)?;
.ok_or(Error::MissingHref(link.inner_text(parser).to_string()))?;
let href = std::str::from_utf8(href.as_bytes())?;

// Extract the hash, which should be in the fragment.
Expand Down Expand Up @@ -187,8 +187,8 @@ pub enum Error {
#[error(transparent)]
HtmlParse(#[from] tl::ParseError),

#[error("Missing href attribute on anchor link")]
MissingHref,
#[error("Missing href attribute on anchor link: `{0}`")]
MissingHref(String),

#[error("Expected distribution filename as last path component of URL: {0}")]
MissingFilename(String),
Expand Down
4 changes: 2 additions & 2 deletions crates/uv-client/src/html/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ fn parse_missing_href() {
";
let base = Url::parse("https://download.pytorch.org/whl/jinja2/").unwrap();
let result = SimpleHtml::parse(text, &base).unwrap_err();
insta::assert_snapshot!(result, @"Missing href attribute on anchor link");
insta::assert_snapshot!(result, @"Missing href attribute on anchor link: `Jinja2-3.1.2-py3-none-any.whl`");
}

#[test]
Expand All @@ -436,7 +436,7 @@ fn parse_empty_href() {
"#;
let base = Url::parse("https://download.pytorch.org/whl/jinja2/").unwrap();
let result = SimpleHtml::parse(text, &base).unwrap_err();
insta::assert_snapshot!(result, @"Missing href attribute on anchor link");
insta::assert_snapshot!(result, @"Missing href attribute on anchor link: `Jinja2-3.1.2-py3-none-any.whl`");
}

#[test]
Expand Down

0 comments on commit 57ff533

Please sign in to comment.