Skip to content

Commit

Permalink
Fix wrap oserrors. (#2442)
Browse files Browse the repository at this point in the history
* Fix wrap oserrors.

* Skip unix socket tests on windows
  • Loading branch information
hellysmile authored and asvetlov committed Oct 30, 2017
1 parent 10e0d97 commit 3604e23
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/2423.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix connector convert OSError to ClientConnectorError
15 changes: 13 additions & 2 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,13 @@ def _create_direct_connection(self, req):
sslcontext = self._get_ssl_context(req)
fingerprint, hashfunc = self._get_fingerprint_and_hashfunc(req)

try:
hosts = yield from self._resolve_host(req.url.raw_host, req.port)
except OSError as exc:
# in case of proxy it is not ClientProxyConnectionError
# it is problem of resolving proxy ip itself
raise ClientConnectorError(req.connection_key, exc) from exc

hosts = yield from self._resolve_host(req.url.raw_host, req.port)

for hinfo in hosts:
Expand Down Expand Up @@ -938,6 +945,10 @@ def path(self):

@asyncio.coroutine
def _create_connection(self, req):
_, proto = yield from self._loop.create_unix_connection(
self._factory, self._path)
try:
_, proto = yield from self._loop.create_unix_connection(
self._factory, self._path)
except OSError as exc:
raise ClientConnectorError(req.connection_key, exc) from exc

return proto
44 changes: 43 additions & 1 deletion tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ssl
import tempfile
import unittest
import uuid
from unittest import mock

import pytest
Expand All @@ -18,7 +19,7 @@
from aiohttp import client, helpers, web
from aiohttp.client import ClientRequest
from aiohttp.connector import Connection, _DNSCacheTable
from aiohttp.test_utils import unused_port
from aiohttp.test_utils import make_mocked_coro, unused_port


@pytest.fixture()
Expand Down Expand Up @@ -507,6 +508,19 @@ def test_tcp_connector_dns_throttle_requests_cancelled_when_close(
yield from f


def test_dns_error(loop):
connector = aiohttp.TCPConnector(loop=loop)
connector._resolve_host = make_mocked_coro(
raise_exception=OSError('dont take it serious'))

req = ClientRequest(
'GET', URL('http://www.python.org'),
loop=loop,
)
with pytest.raises(aiohttp.ClientConnectorError):
loop.run_until_complete(connector.connect(req))


def test_get_pop_empty_conns(loop):
# see issue #473
conn = aiohttp.BaseConnector(loop=loop)
Expand Down Expand Up @@ -1281,6 +1295,34 @@ def handler(request):
assert r.status == 200


@pytest.mark.skipif(not hasattr(socket, 'AF_UNIX'),
reason="requires unix socket")
def test_unix_connector_not_found(loop):
connector = aiohttp.UnixConnector('/' + uuid.uuid4().hex, loop=loop)

req = ClientRequest(
'GET', URL('http://www.python.org'),
loop=loop,
)
with pytest.raises(aiohttp.ClientConnectorError):
loop.run_until_complete(connector.connect(req))


@pytest.mark.skipif(not hasattr(socket, 'AF_UNIX'),
reason="requires unix socket")
def test_unix_connector_permission(loop):
loop.create_unix_connection = make_mocked_coro(
raise_exception=PermissionError())
connector = aiohttp.UnixConnector('/' + uuid.uuid4().hex, loop=loop)

req = ClientRequest(
'GET', URL('http://www.python.org'),
loop=loop,
)
with pytest.raises(aiohttp.ClientConnectorError):
loop.run_until_complete(connector.connect(req))


def test_default_use_dns_cache(loop):
conn = aiohttp.TCPConnector(loop=loop)
assert conn.use_dns_cache
Expand Down
2 changes: 1 addition & 1 deletion tests/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def _test_connect_request_with_unicode_host(self, Request_mock):
Request_mock.assert_called_with(mock.ANY, mock.ANY, "xn--9caa.com:80",
mock.ANY, loop=loop)

def test_proxy_connection_error(self):
def test_proxy_dns_error(self):
connector = aiohttp.TCPConnector(loop=self.loop)
connector._resolve_host = make_mocked_coro(
raise_exception=OSError('dont take it serious'))
Expand Down

0 comments on commit 3604e23

Please sign in to comment.