Skip to content

Commit ba9b33e

Browse files
authored
[PR #9451/216e082 backport][3.10] Fix AsyncResolver swallowing the error message (#9452)
1 parent bc9e5d6 commit ba9b33e

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

CHANGES/9451.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed error messages from :py:class:`~aiohttp.resolver.AsyncResolver` being swallowed -- by :user:`bdraco`.

aiohttp/resolver.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async def resolve(
111111
)
112112
except aiodns.error.DNSError as exc:
113113
msg = exc.args[1] if len(exc.args) >= 1 else "DNS lookup failed"
114-
raise OSError(msg) from exc
114+
raise OSError(None, msg) from exc
115115
hosts: List[ResolveResult] = []
116116
for node in resp.nodes:
117117
address: Union[Tuple[bytes, int], Tuple[bytes, int, int, int]] = node.addr
@@ -145,7 +145,7 @@ async def resolve(
145145
)
146146

147147
if not hosts:
148-
raise OSError("DNS lookup failed")
148+
raise OSError(None, "DNS lookup failed")
149149

150150
return hosts
151151

tests/test_resolver.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,35 @@ async def test_async_resolver_query_ipv6_positive_lookup(loop) -> None:
339339
mock().query.assert_called_with("www.python.org", "AAAA")
340340

341341

342-
async def test_async_resolver_aiodns_not_present(loop, monkeypatch) -> None:
342+
@pytest.mark.skipif(not getaddrinfo, reason="aiodns >=3.2.0 required")
343+
async def test_async_resolver_error_messages_passed(
344+
loop: asyncio.AbstractEventLoop,
345+
) -> None:
346+
"""Ensure error messages are passed through from aiodns."""
347+
with patch("aiodns.DNSResolver", autospec=True, spec_set=True) as mock:
348+
mock().getaddrinfo.side_effect = aiodns.error.DNSError(1, "Test error message")
349+
resolver = AsyncResolver()
350+
with pytest.raises(OSError, match="Test error message") as excinfo:
351+
await resolver.resolve("x.org")
352+
353+
assert excinfo.value.strerror == "Test error message"
354+
355+
356+
@pytest.mark.skipif(not getaddrinfo, reason="aiodns >=3.2.0 required")
357+
async def test_async_resolver_error_messages_passed_no_hosts(
358+
loop: asyncio.AbstractEventLoop,
359+
) -> None:
360+
"""Ensure error messages are passed through from aiodns."""
361+
with patch("aiodns.DNSResolver", autospec=True, spec_set=True) as mock:
362+
mock().getaddrinfo.return_value = fake_aiodns_getaddrinfo_ipv6_result([])
363+
resolver = AsyncResolver()
364+
with pytest.raises(OSError, match="DNS lookup failed") as excinfo:
365+
await resolver.resolve("x.org")
366+
367+
assert excinfo.value.strerror == "DNS lookup failed"
368+
369+
370+
async def test_async_resolver_aiodns_not_present(loop: Any, monkeypatch: Any) -> None:
343371
monkeypatch.setattr("aiohttp.resolver.aiodns", None)
344372
with pytest.raises(RuntimeError):
345373
AsyncResolver(loop=loop)

0 commit comments

Comments
 (0)