Skip to content

Commit

Permalink
[PR #9455/dfaafac0 backport][3.10] Fix AsyncResolver query fallback s…
Browse files Browse the repository at this point in the history
…wallowing the error message (#9456)

Co-authored-by: J. Nick Koston <nick@koston.org>
  • Loading branch information
patchback[bot] and bdraco authored Oct 10, 2024
1 parent ba9b33e commit ee87a04
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/9455.bugfix.rst
4 changes: 2 additions & 2 deletions aiohttp/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async def _resolve_with_query(
resp = await self._resolver.query(host, qtype)
except aiodns.error.DNSError as exc:
msg = exc.args[1] if len(exc.args) >= 1 else "DNS lookup failed"
raise OSError(msg) from exc
raise OSError(None, msg) from exc

hosts = []
for rr in resp:
Expand All @@ -177,7 +177,7 @@ async def _resolve_with_query(
)

if not hosts:
raise OSError("DNS lookup failed")
raise OSError(None, "DNS lookup failed")

return hosts

Expand Down
30 changes: 30 additions & 0 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,36 @@ async def test_async_resolver_query_ipv6_positive_lookup(loop) -> None:
mock().query.assert_called_with("www.python.org", "AAAA")


@pytest.mark.skipif(not getaddrinfo, reason="aiodns >=3.2.0 required")
async def test_async_resolver_query_fallback_error_messages_passed(
loop: asyncio.AbstractEventLoop,
) -> None:
"""Ensure error messages are passed through from aiodns with query fallback."""
with patch("aiodns.DNSResolver", autospec=True, spec_set=True) as mock:
del mock().gethostbyname
mock().query.side_effect = aiodns.error.DNSError(1, "Test error message")
resolver = AsyncResolver()
with pytest.raises(OSError, match="Test error message") as excinfo:
await resolver.resolve("x.org")

assert excinfo.value.strerror == "Test error message"


@pytest.mark.skipif(not getaddrinfo, reason="aiodns >=3.2.0 required")
async def test_async_resolver_query_fallback_error_messages_passed_no_hosts(
loop: asyncio.AbstractEventLoop,
) -> None:
"""Ensure error messages are passed through from aiodns with query fallback."""
with patch("aiodns.DNSResolver", autospec=True, spec_set=True) as mock:
del mock().gethostbyname
mock().query.return_value = fake_query_result([])
resolver = AsyncResolver()
with pytest.raises(OSError, match="DNS lookup failed") as excinfo:
await resolver.resolve("x.org")

assert excinfo.value.strerror == "DNS lookup failed"


@pytest.mark.skipif(not getaddrinfo, reason="aiodns >=3.2.0 required")
async def test_async_resolver_error_messages_passed(
loop: asyncio.AbstractEventLoop,
Expand Down

0 comments on commit ee87a04

Please sign in to comment.