Except IOError and not ConnectionError

PySocks' errors do not subclass ConnectionError so the library
was unnecessarily logging them as unexpected, when any IOError
was actually expected.
This commit is contained in:
Lonami Exo 2019-03-28 09:52:46 +01:00
parent c902428af1
commit 7523869875
3 changed files with 10 additions and 7 deletions

View File

@ -144,7 +144,7 @@ class Connection(abc.ABC):
except asyncio.CancelledError: except asyncio.CancelledError:
pass pass
except Exception as e: except Exception as e:
if isinstance(e, ConnectionError): if isinstance(e, IOError):
self._log.info('The server closed the connection while sending') self._log.info('The server closed the connection while sending')
else: else:
self._log.exception('Unexpected exception in the send loop') self._log.exception('Unexpected exception in the send loop')
@ -161,7 +161,7 @@ class Connection(abc.ABC):
except asyncio.CancelledError: except asyncio.CancelledError:
break break
except Exception as e: except Exception as e:
if isinstance(e, (ConnectionError, asyncio.IncompleteReadError)): if isinstance(e, (IOError, asyncio.IncompleteReadError)):
msg = 'The server closed the connection' msg = 'The server closed the connection'
self._log.info(msg) self._log.info(msg)
elif isinstance(e, InvalidChecksumError): elif isinstance(e, InvalidChecksumError):

View File

@ -198,7 +198,7 @@ class MTProtoSender:
try: try:
self._log.debug('Connection attempt {}...'.format(attempt)) self._log.debug('Connection attempt {}...'.format(attempt))
await self._connection.connect(timeout=self._connect_timeout) await self._connection.connect(timeout=self._connect_timeout)
except (ConnectionError, asyncio.TimeoutError) as e: except (IOError, asyncio.TimeoutError) as e:
self._log.warning('Attempt {} at connecting failed: {}: {}' self._log.warning('Attempt {} at connecting failed: {}: {}'
.format(attempt, type(e).__name__, e)) .format(attempt, type(e).__name__, e))
await asyncio.sleep(self._delay) await asyncio.sleep(self._delay)
@ -308,7 +308,7 @@ class MTProtoSender:
for attempt in retry_range(retries): for attempt in retry_range(retries):
try: try:
await self._connect() await self._connect()
except (ConnectionError, asyncio.TimeoutError) as e: except (IOError, asyncio.TimeoutError) as e:
self._log.info('Failed reconnection attempt %d with %s', self._log.info('Failed reconnection attempt %d with %s',
attempt, e.__class__.__name__) attempt, e.__class__.__name__)
@ -377,7 +377,7 @@ class MTProtoSender:
data = self._state.encrypt_message_data(data) data = self._state.encrypt_message_data(data)
try: try:
await self._connection.send(data) await self._connection.send(data)
except ConnectionError: except IOError:
self._log.info('Connection closed while sending data') self._log.info('Connection closed while sending data')
self._start_reconnect() self._start_reconnect()
return return
@ -404,7 +404,7 @@ class MTProtoSender:
self._log.debug('Receiving items from the network...') self._log.debug('Receiving items from the network...')
try: try:
body = await self._connection.recv() body = await self._connection.recv()
except ConnectionError: except IOError:
self._log.info('Connection closed while receiving data') self._log.info('Connection closed while receiving data')
self._start_reconnect() self._start_reconnect()
return return

View File

@ -115,7 +115,10 @@ class InteractiveTelegramClient(TelegramClient):
print('Connecting to Telegram servers...') print('Connecting to Telegram servers...')
try: try:
loop.run_until_complete(self.connect()) loop.run_until_complete(self.connect())
except ConnectionError: except IOError:
# We handle IOError and not ConnectionError because
# PySocks' errors do not subclass ConnectionError
# (so this will work with and without proxies).
print('Initial connection failed. Retrying...') print('Initial connection failed. Retrying...')
loop.run_until_complete(self.connect()) loop.run_until_complete(self.connect())