Skip to content

Commit

Permalink
handle network errors and arbitrary errors during title resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonDavis committed Nov 26, 2024
1 parent 3bb1acc commit 0fcf2ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion sde_collections/models/delta_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def generate_title_for_url(self, url_obj) -> tuple[str, str | None]:

try:
return resolve_title(self.title_pattern, context), None
except (ValueError, ValidationError) as e:
except Exception as e:
return None, str(e)

def apply(self) -> None:
Expand Down
46 changes: 25 additions & 21 deletions sde_collections/utils/title_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,33 @@ def resolve_xpath(xpath: str, url: str) -> str:
if not is_valid_xpath(xpath):
raise ValueError(f"The xpath, {xpath}, is not valid.")

response = requests.get(url)

if response.ok:
tree = html.fromstring(response.content)
values = tree.xpath(xpath)

if len(values) == 1:
if isinstance(values[0], str):
text_content = values[0]
else:
text_content = values[0].text

if text_content:
text_content = clean_text(text_content)
return text_content
try:
response = requests.get(url)

if response.ok:
tree = html.fromstring(response.content)
values = tree.xpath(xpath)

if len(values) == 1:
if isinstance(values[0], str):
text_content = values[0]
else:
text_content = values[0].text

if text_content:
text_content = clean_text(text_content)
return text_content
else:
raise ValueError(f"The element at the xpath, {xpath}, does not contain any text content.")
elif len(values) > 1:
raise ValueError(f"More than one element found for the xpath, {xpath}")
else:
raise ValueError(f"The element at the xpath, {xpath}, does not contain any text content.")
elif len(values) > 1:
raise ValueError(f"More than one element found for the xpath, {xpath}")
raise ValueError(f"No element found for the xpath, {xpath}")
else:
raise ValueError(f"No element found for the xpath, {xpath}")
else:
raise ValueError(f"Failed to retrieve the {url}. Status code: {response.status_code}")
raise ValueError(f"Failed to retrieve the {url}. Status code: {response.status_code}")

except requests.RequestException as e:
raise ValueError(f"Network error while accessing {url}: {str(e)}")


def parse_title(input_string: str) -> list[tuple[str, str]]:
Expand Down

0 comments on commit 0fcf2ea

Please sign in to comment.