Skip to content

Commit

Permalink
[3.11] Fix AsyncResolver query fallback swallowing the error message
Browse files Browse the repository at this point in the history
Same change as #9451 but for the query fallback code for older
versions of aiodns
  • Loading branch information
bdraco committed Oct 10, 2024
1 parent 42d30d5 commit dbaa817
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions aiohttp/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,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 @@ -175,7 +175,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 @@ -332,6 +332,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 dbaa817

Please sign in to comment.