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

View File

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

View File

@ -115,7 +115,10 @@ class InteractiveTelegramClient(TelegramClient):
print('Connecting to Telegram servers...')
try:
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...')
loop.run_until_complete(self.connect())