Skip to content

Commit 0fcf2ea

Browse files
committed
handle network errors and arbitrary errors during title resolution
1 parent 3bb1acc commit 0fcf2ea

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

sde_collections/models/delta_patterns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ def generate_title_for_url(self, url_obj) -> tuple[str, str | None]:
425425

426426
try:
427427
return resolve_title(self.title_pattern, context), None
428-
except (ValueError, ValidationError) as e:
428+
except Exception as e:
429429
return None, str(e)
430430

431431
def apply(self) -> None:

sde_collections/utils/title_resolver.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,33 @@ def resolve_xpath(xpath: str, url: str) -> str:
6363
if not is_valid_xpath(xpath):
6464
raise ValueError(f"The xpath, {xpath}, is not valid.")
6565

66-
response = requests.get(url)
67-
68-
if response.ok:
69-
tree = html.fromstring(response.content)
70-
values = tree.xpath(xpath)
71-
72-
if len(values) == 1:
73-
if isinstance(values[0], str):
74-
text_content = values[0]
75-
else:
76-
text_content = values[0].text
77-
78-
if text_content:
79-
text_content = clean_text(text_content)
80-
return text_content
66+
try:
67+
response = requests.get(url)
68+
69+
if response.ok:
70+
tree = html.fromstring(response.content)
71+
values = tree.xpath(xpath)
72+
73+
if len(values) == 1:
74+
if isinstance(values[0], str):
75+
text_content = values[0]
76+
else:
77+
text_content = values[0].text
78+
79+
if text_content:
80+
text_content = clean_text(text_content)
81+
return text_content
82+
else:
83+
raise ValueError(f"The element at the xpath, {xpath}, does not contain any text content.")
84+
elif len(values) > 1:
85+
raise ValueError(f"More than one element found for the xpath, {xpath}")
8186
else:
82-
raise ValueError(f"The element at the xpath, {xpath}, does not contain any text content.")
83-
elif len(values) > 1:
84-
raise ValueError(f"More than one element found for the xpath, {xpath}")
87+
raise ValueError(f"No element found for the xpath, {xpath}")
8588
else:
86-
raise ValueError(f"No element found for the xpath, {xpath}")
87-
else:
88-
raise ValueError(f"Failed to retrieve the {url}. Status code: {response.status_code}")
89+
raise ValueError(f"Failed to retrieve the {url}. Status code: {response.status_code}")
90+
91+
except requests.RequestException as e:
92+
raise ValueError(f"Network error while accessing {url}: {str(e)}")
8993

9094

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

0 commit comments

Comments
 (0)