Improve Error Message for NetworkError (#3505)

This commit is contained in:
Bibo-Joshi 2023-01-15 13:40:20 +01:00 committed by GitHub
parent 4ebcec2b91
commit 3dd7d84fa5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 5 deletions

View file

@ -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

View file

@ -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",