Skip to content

Increase visibility of error message when action fails due to HTTP request error #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ filterwarnings =
ignore::DeprecationWarning
ignore::ResourceWarning

# --capture=no - disable per-test capture
# --tb=long sets the length of the traceback in case of failures
addopts = --capture=no --tb=long --verbose
addopts = --tb=long --verbose
pythonpath = reportsizedeltas
23 changes: 15 additions & 8 deletions reportsizedeltas/reportsizedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,19 +630,26 @@ def raw_http_request(self, url: str, data: bytes | None = None):
request = urllib.request.Request(url=url, headers=headers, data=data)

retry_count = 0
while retry_count <= maximum_urlopen_retries:
retry_count += 1
while True:
try:
# The rate limit API is not subject to rate limiting
if url.startswith("https://api.github.com") and not url.startswith("https://api.github.com/rate_limit"):
self.handle_rate_limiting()
return urllib.request.urlopen(url=request)
except Exception as exception:
if not determine_urlopen_retry(exception=exception):
raise exception
except urllib.error.HTTPError as exception:
if determine_urlopen_retry(exception=exception):
if retry_count < maximum_urlopen_retries:
retry_count += 1
continue
else:
# Maximum retries reached without successfully opening URL
print("Maximum number of URL load retries exceeded")

print(f"::error::{exception.__class__.__name__}: {exception}")
for line in exception.fp:
print(line.decode(encoding="utf-8", errors="ignore"))

# Maximum retries reached without successfully opening URL
raise TimeoutError("Maximum number of URL load retries exceeded")
raise exception

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


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

Expand Down
8 changes: 5 additions & 3 deletions reportsizedeltas/tests/test_reportsizedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,14 +901,16 @@ def test_raw_http_request(mocker):
urllib.request.urlopen.assert_called_once_with(url=request)

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

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


Expand Down