Skip to content

Commit 2537157

Browse files
committed
Restrict HTTP request error handling to appropriate exception
The "arduino/report-size-deltas" action relies on the GitHub REST API. It makes HTTP requests to various API endpoints during each workflow run. It is expected that these requests might occasionally produce an exception due to transient network outages. For this reason, the action has special handling of exceptions. Previously, that exception handling code was configured to be triggered on any exception, even though it is specific to HTTP request errors. It is more appropriate to target it only to the specific urllib.error.HTTPError exception that results from an HTTP request error, leaving any other types of unexpected exceptions unhandled.
1 parent e9c0345 commit 2537157

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

reportsizedeltas/reportsizedeltas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def raw_http_request(self, url: str, data: bytes | None = None):
637637
if url.startswith("https://api.github.com") and not url.startswith("https://api.github.com/rate_limit"):
638638
self.handle_rate_limiting()
639639
return urllib.request.urlopen(url=request)
640-
except Exception as exception:
640+
except urllib.error.HTTPError as exception:
641641
if not determine_urlopen_retry(exception=exception):
642642
raise exception
643643

@@ -664,7 +664,7 @@ def handle_rate_limiting(self) -> None:
664664
sys.exit(0)
665665

666666

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

reportsizedeltas/tests/test_reportsizedeltas.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -901,9 +901,11 @@ 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

0 commit comments

Comments
 (0)