From 3dd7d84fa5d87ad8121129c7074f5b3430c51cdf Mon Sep 17 00:00:00 2001 From: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sun, 15 Jan 2023 13:40:20 +0100 Subject: [PATCH] Improve Error Message for `NetworkError` (#3505) --- telegram/request/_httpxrequest.py | 5 ++++- tests/test_request.py | 11 +++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/telegram/request/_httpxrequest.py b/telegram/request/_httpxrequest.py index 8c9c2f9ac..41cc8b21d 100644 --- a/telegram/request/_httpxrequest.py +++ b/telegram/request/_httpxrequest.py @@ -201,6 +201,9 @@ class HTTPXRequest(BaseRequest): except httpx.HTTPError as err: # HTTPError must come last as its the base httpx exception class # TODO p4: do something smart here; for now just raise NetworkError - raise NetworkError(f"httpx HTTPError: {err}") from err + + # We include the class name for easier debugging. Especially useful if the error + # message of `err` is empty. + raise NetworkError(f"httpx.{err.__class__.__name__}: {err}") from err return res.status_code, res.content diff --git a/tests/test_request.py b/tests/test_request.py index 3a1659245..af074f842 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -533,18 +533,21 @@ class TestHTTPXRequest: assert content == b"content" @pytest.mark.parametrize( - ["raised_class", "expected_class"], - [(httpx.TimeoutException, TimedOut), (httpx.HTTPError, NetworkError)], + ["raised_class", "expected_class", "expected_message"], + [ + (httpx.TimeoutException, TimedOut, "Timed out"), + (httpx.ReadError, NetworkError, "httpx.ReadError: message"), + ], ) async def test_do_request_exceptions( - self, monkeypatch, httpx_request, raised_class, expected_class + self, monkeypatch, httpx_request, raised_class, expected_class, expected_message ): async def make_assertion(self, method, url, headers, timeout, files, data): raise raised_class("message") monkeypatch.setattr(httpx.AsyncClient, "request", make_assertion) - with pytest.raises(expected_class): + with pytest.raises(expected_class, match=expected_message): await httpx_request.do_request( "method", "url",