Skip to content

Commit

Permalink
[networking] Fix handler not being added to RequestError (yt-dlp#10955)
Browse files Browse the repository at this point in the history
Authored by: coletdjnz
  • Loading branch information
coletdjnz authored Sep 8, 2024
1 parent 46f4c80 commit d1c4d88
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions test/test_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,24 @@ def test_remove_logging_handler(self, handler, logger_name):
rh.close()
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)
class TestUrllibRequestHandler(TestRequestHandlerBase):
Expand Down
4 changes: 2 additions & 2 deletions yt_dlp/networking/_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import urllib.parse
import urllib.request

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

0 comments on commit d1c4d88

Please sign in to comment.