[networking] Fix handler not being added to RequestError (#10955)

Authored by: coletdjnz
pull/10963/head^2
coletdjnz 2 months ago committed by GitHub
parent 46f4c80bc3
commit d1c4d88b2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -822,6 +822,24 @@ class TestRequestHandlerMisc:
rh.close() rh.close()
assert len(logging_handlers) == before_count assert len(logging_handlers) == before_count
def test_wrap_request_errors(self):
class TestRequestHandler(RequestHandler):
def _validate(self, request):
if request.headers.get('x-fail'):
raise UnsupportedRequest('test error')
def _send(self, request: Request):
raise RequestError('test error')
with TestRequestHandler(logger=FakeLogger()) as rh:
with pytest.raises(UnsupportedRequest, match='test error') as exc_info:
rh.validate(Request('http://example.com', headers={'x-fail': '1'}))
assert exc_info.value.handler is rh
with pytest.raises(RequestError, match='test error') as exc_info:
rh.send(Request('http://example.com'))
assert exc_info.value.handler is rh
@pytest.mark.parametrize('handler', ['Urllib'], indirect=True) @pytest.mark.parametrize('handler', ['Urllib'], indirect=True)
class TestUrllibRequestHandler(TestRequestHandlerBase): class TestUrllibRequestHandler(TestRequestHandlerBase):

@ -10,7 +10,7 @@ import typing
import urllib.parse import urllib.parse
import urllib.request import urllib.request
from .exceptions import RequestError, UnsupportedRequest from .exceptions import RequestError
from ..dependencies import certifi from ..dependencies import certifi
from ..socks import ProxyType, sockssocket from ..socks import ProxyType, sockssocket
from ..utils import format_field, traverse_obj from ..utils import format_field, traverse_obj
@ -206,7 +206,7 @@ def wrap_request_errors(func):
def wrapper(self, *args, **kwargs): def wrapper(self, *args, **kwargs):
try: try:
return func(self, *args, **kwargs) return func(self, *args, **kwargs)
except UnsupportedRequest as e: except RequestError as e:
if e.handler is None: if e.handler is None:
e.handler = self e.handler = self
raise raise

Loading…
Cancel
Save