From 24cd6ec506ae04b986d760fb8d02ffce28946556 Mon Sep 17 00:00:00 2001 From: tboy1337 Date: Tue, 21 Oct 2025 14:12:57 +0100 Subject: [PATCH 1/2] Enhance error handling in HTTPAdapter to wrap unknown HTTPError types in RequestException. Added unit test to verify behavior for unhandled urllib3 HTTPError types. --- src/requests/adapters.py | 17 +++++++++-------- tests/test_adapters.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/requests/adapters.py b/src/requests/adapters.py index 670c92767c..4c994fcb1e 100644 --- a/src/requests/adapters.py +++ b/src/requests/adapters.py @@ -40,6 +40,7 @@ InvalidURL, ProxyError, ReadTimeout, + RequestException, RetryError, SSLError, ) @@ -682,15 +683,15 @@ def send( except _ProxyError as e: raise ProxyError(e) - except (_SSLError, _HTTPError) as e: - if isinstance(e, _SSLError): + except (_SSLError, _HTTPError) as err: + if isinstance(err, _SSLError): # This branch is for urllib3 versions earlier than v1.22 - raise SSLError(e, request=request) - elif isinstance(e, ReadTimeoutError): - raise ReadTimeout(e, request=request) - elif isinstance(e, _InvalidHeader): - raise InvalidHeader(e, request=request) + raise SSLError(err, request=request) from err + elif isinstance(err, ReadTimeoutError): + raise ReadTimeout(err, request=request) from err + elif isinstance(err, _InvalidHeader): + raise InvalidHeader(err, request=request) from err else: - raise + raise RequestException(err, request=request) from err return self.build_response(request, resp) diff --git a/tests/test_adapters.py b/tests/test_adapters.py index 6c55d5a130..51e593e735 100644 --- a/tests/test_adapters.py +++ b/tests/test_adapters.py @@ -1,4 +1,10 @@ +from unittest import mock + +import pytest +from urllib3.exceptions import HTTPError as _HTTPError + import requests.adapters +from requests.exceptions import RequestException def test_request_url_trims_leading_path_separators(): @@ -6,3 +12,34 @@ def test_request_url_trims_leading_path_separators(): a = requests.adapters.HTTPAdapter() p = requests.Request(method="GET", url="http://127.0.0.1:10000//v:h").prepare() assert "/v:h" == a.request_url(p, {}) + + +def test_unknown_httperror_wrapped_in_requestexception(): + """Test that unknown urllib3 HTTPError types are wrapped in RequestException. + + This tests Bug #007: When urllib3 raises an HTTPError that isn't explicitly + handled (not SSLError, ReadTimeoutError, or InvalidHeader), it should be + wrapped in RequestException rather than propagating as-is. + """ + adapter = requests.adapters.HTTPAdapter() + request = requests.Request(method="GET", url="http://example.com").prepare() + + # Create a generic HTTPError that doesn't match any specific handled types + generic_http_error = _HTTPError("Unknown HTTP error from urllib3") + + # Mock the connection to raise our generic HTTPError + with mock.patch.object( + adapter, 'get_connection_with_tls_context' + ) as mock_get_conn: + mock_conn = mock.MagicMock() + mock_conn.urlopen.side_effect = generic_http_error + mock_get_conn.return_value = mock_conn + + # The exception should be catchable as RequestException + with pytest.raises(RequestException) as exc_info: + adapter.send(request) + + # Verify it wraps the original error + assert exc_info.value.request == request + # Verify exception chaining + assert exc_info.value.__cause__ == generic_http_error From 2fde261e2a2c01fcee9e725d3c8abf029ed70dbf Mon Sep 17 00:00:00 2001 From: tboy1337 Date: Tue, 21 Oct 2025 18:18:38 +0100 Subject: [PATCH 2/2] Refactor test documentation for clarity in test_unknown_httperror_wrapped_in_requestexception. Removed reference to Bug #007. --- tests/test_adapters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_adapters.py b/tests/test_adapters.py index 51e593e735..fdda8418d9 100644 --- a/tests/test_adapters.py +++ b/tests/test_adapters.py @@ -17,7 +17,7 @@ def test_request_url_trims_leading_path_separators(): def test_unknown_httperror_wrapped_in_requestexception(): """Test that unknown urllib3 HTTPError types are wrapped in RequestException. - This tests Bug #007: When urllib3 raises an HTTPError that isn't explicitly + When urllib3 raises an HTTPError that isn't explicitly handled (not SSLError, ReadTimeoutError, or InvalidHeader), it should be wrapped in RequestException rather than propagating as-is. """