Skip to content

Commit 8a0046b

Browse files
authored
Merge pull request #81 from per1234/improve-http-error-messages
Increase visibility of error message when action fails due to HTTP request error
2 parents e9c0345 + 0149266 commit 8a0046b

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

pytest.ini

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ filterwarnings =
55
ignore::DeprecationWarning
66
ignore::ResourceWarning
77

8-
# --capture=no - disable per-test capture
98
# --tb=long sets the length of the traceback in case of failures
10-
addopts = --capture=no --tb=long --verbose
9+
addopts = --tb=long --verbose
1110
pythonpath = reportsizedeltas

reportsizedeltas/reportsizedeltas.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -630,19 +630,26 @@ def raw_http_request(self, url: str, data: bytes | None = None):
630630
request = urllib.request.Request(url=url, headers=headers, data=data)
631631

632632
retry_count = 0
633-
while retry_count <= maximum_urlopen_retries:
634-
retry_count += 1
633+
while True:
635634
try:
636635
# The rate limit API is not subject to rate limiting
637636
if url.startswith("https://api.github.com") and not url.startswith("https://api.github.com/rate_limit"):
638637
self.handle_rate_limiting()
639638
return urllib.request.urlopen(url=request)
640-
except Exception as exception:
641-
if not determine_urlopen_retry(exception=exception):
642-
raise exception
639+
except urllib.error.HTTPError as exception:
640+
if determine_urlopen_retry(exception=exception):
641+
if retry_count < maximum_urlopen_retries:
642+
retry_count += 1
643+
continue
644+
else:
645+
# Maximum retries reached without successfully opening URL
646+
print("Maximum number of URL load retries exceeded")
647+
648+
print(f"::error::{exception.__class__.__name__}: {exception}")
649+
for line in exception.fp:
650+
print(line.decode(encoding="utf-8", errors="ignore"))
643651

644-
# Maximum retries reached without successfully opening URL
645-
raise TimeoutError("Maximum number of URL load retries exceeded")
652+
raise exception
646653

647654
def handle_rate_limiting(self) -> None:
648655
"""Check whether the GitHub API request limit has been reached.
@@ -664,7 +671,7 @@ def handle_rate_limiting(self) -> None:
664671
sys.exit(0)
665672

666673

667-
def determine_urlopen_retry(exception) -> bool:
674+
def determine_urlopen_retry(exception: urllib.error.HTTPError) -> bool:
668675
"""Determine whether the exception warrants another attempt at opening the URL.
669676
If so, delay then return True. Otherwise, return False.
670677

reportsizedeltas/tests/test_reportsizedeltas.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -901,14 +901,16 @@ def test_raw_http_request(mocker):
901901
urllib.request.urlopen.assert_called_once_with(url=request)
902902

903903
# urllib.request.urlopen() has non-recoverable exception
904-
urllib.request.urlopen.side_effect = Exception()
904+
urllib.request.urlopen.side_effect = urllib.error.HTTPError(
905+
url="http://example.com", code=404, msg="", hdrs=None, fp=None
906+
)
905907
mocker.patch("reportsizedeltas.determine_urlopen_retry", autospec=True, return_value=False)
906-
with pytest.raises(expected_exception=Exception):
908+
with pytest.raises(expected_exception=urllib.error.HTTPError):
907909
report_size_deltas.raw_http_request(url=url, data=data)
908910

909911
# urllib.request.urlopen() has potentially recoverable exceptions, but exceeds retry count
910912
reportsizedeltas.determine_urlopen_retry.return_value = True
911-
with pytest.raises(expected_exception=TimeoutError):
913+
with pytest.raises(expected_exception=urllib.error.HTTPError):
912914
report_size_deltas.raw_http_request(url=url, data=data)
913915

914916

0 commit comments

Comments
 (0)