Skip to content

Commit

Permalink
change error handling to MaxRetryError in _fetch()
Browse files Browse the repository at this point in the history
Signed-off-by: NicholasTanz <[email protected]>
  • Loading branch information
NicholasTanz committed Jan 10, 2025
1 parent 2128030 commit 2aed81f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
12 changes: 6 additions & 6 deletions tests/test_fetcher_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ def test_http_error(self) -> None:
def test_response_read_timeout(self, mock_session_get: Mock) -> None:
mock_response = Mock()
mock_response.status = 200
attr = {
"stream.side_effect": urllib3.exceptions.ConnectionError(
"Simulated timeout"
)
}
attr = {"stream.side_effect": urllib3.exceptions.TimeoutError}
mock_response.configure_mock(**attr)
mock_session_get.return_value = mock_response

Expand All @@ -125,7 +121,11 @@ def test_response_read_timeout(self, mock_session_get: Mock) -> None:
@patch.object(
urllib3.PoolManager,
"request",
side_effect=urllib3.exceptions.TimeoutError,
side_effect=urllib3.exceptions.MaxRetryError(
urllib3.connectionpool.ConnectionPool("localhost"),
"",
urllib3.exceptions.TimeoutError(),
),
)
def test_session_get_timeout(self, mock_session_get: Mock) -> None:
with self.assertRaises(exceptions.SlowRetrievalError):
Expand Down
10 changes: 4 additions & 6 deletions tuf/ngclient/_internal/urllib3_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ def _fetch(self, url: str) -> Iterator[bytes]:
preload_content=False,
timeout=urllib3.Timeout(self.socket_timeout),
)
except urllib3.exceptions.TimeoutError as e:
raise exceptions.SlowRetrievalError from e
except urllib3.exceptions.MaxRetryError as e:
if isinstance(e.reason, urllib3.exceptions.TimeoutError):
raise exceptions.SlowRetrievalError from e

if response.status >= 400:
response.close()
Expand All @@ -102,10 +103,7 @@ def _chunks(

try:
yield from response.stream(self.chunk_size)
except (
urllib3.exceptions.ConnectionError,
urllib3.exceptions.TimeoutError,
) as e:
except urllib3.exceptions.TimeoutError as e:
raise exceptions.SlowRetrievalError from e

finally:
Expand Down

0 comments on commit 2aed81f

Please sign in to comment.