mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-25 08:37:07 +01:00
Improve Error Message for NetworkError
(#3505)
This commit is contained in:
parent
4ebcec2b91
commit
3dd7d84fa5
2 changed files with 11 additions and 5 deletions
|
@ -201,6 +201,9 @@ class HTTPXRequest(BaseRequest):
|
||||||
except httpx.HTTPError as err:
|
except httpx.HTTPError as err:
|
||||||
# HTTPError must come last as its the base httpx exception class
|
# HTTPError must come last as its the base httpx exception class
|
||||||
# TODO p4: do something smart here; for now just raise NetworkError
|
# 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
|
return res.status_code, res.content
|
||||||
|
|
|
@ -533,18 +533,21 @@ class TestHTTPXRequest:
|
||||||
assert content == b"content"
|
assert content == b"content"
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
["raised_class", "expected_class"],
|
["raised_class", "expected_class", "expected_message"],
|
||||||
[(httpx.TimeoutException, TimedOut), (httpx.HTTPError, NetworkError)],
|
[
|
||||||
|
(httpx.TimeoutException, TimedOut, "Timed out"),
|
||||||
|
(httpx.ReadError, NetworkError, "httpx.ReadError: message"),
|
||||||
|
],
|
||||||
)
|
)
|
||||||
async def test_do_request_exceptions(
|
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):
|
async def make_assertion(self, method, url, headers, timeout, files, data):
|
||||||
raise raised_class("message")
|
raise raised_class("message")
|
||||||
|
|
||||||
monkeypatch.setattr(httpx.AsyncClient, "request", make_assertion)
|
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(
|
await httpx_request.do_request(
|
||||||
"method",
|
"method",
|
||||||
"url",
|
"url",
|
||||||
|
|
Loading…
Reference in a new issue