mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-26 03:13:45 +03:00
Handle the right errors
This commit is contained in:
parent
b93b01cb02
commit
9dc4009152
|
@ -111,6 +111,4 @@ class MessagePacker:
|
||||||
s.container_id = container_id
|
s.container_id = container_id
|
||||||
|
|
||||||
data = buffer.getvalue()
|
data = buffer.getvalue()
|
||||||
__log__.debug('Packed %d message(s) in %d bytes for sending',
|
|
||||||
len(batch), len(data))
|
|
||||||
return batch, data
|
return batch, data
|
||||||
|
|
|
@ -4,6 +4,8 @@ import logging
|
||||||
import socket
|
import socket
|
||||||
import ssl as ssl_mod
|
import ssl as ssl_mod
|
||||||
|
|
||||||
|
from ...errors import InvalidChecksumError
|
||||||
|
|
||||||
__log__ = logging.getLogger(__name__)
|
__log__ = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -173,6 +175,9 @@ class Connection(abc.ABC):
|
||||||
if isinstance(e, asyncio.IncompleteReadError):
|
if isinstance(e, asyncio.IncompleteReadError):
|
||||||
msg = 'The server closed the connection'
|
msg = 'The server closed the connection'
|
||||||
logging.info(msg)
|
logging.info(msg)
|
||||||
|
elif isinstance(e, InvalidChecksumError):
|
||||||
|
msg = 'The server response had an invalid checksum'
|
||||||
|
logging.info(msg)
|
||||||
else:
|
else:
|
||||||
msg = 'Unexpected exception in the receive loop'
|
msg = 'Unexpected exception in the receive loop'
|
||||||
logging.exception(msg)
|
logging.exception(msg)
|
||||||
|
|
|
@ -196,7 +196,7 @@ class MTProtoSender:
|
||||||
try:
|
try:
|
||||||
__log__.debug('Connection attempt {}...'.format(retry))
|
__log__.debug('Connection attempt {}...'.format(retry))
|
||||||
await self._connection.connect(timeout=self._connect_timeout)
|
await self._connection.connect(timeout=self._connect_timeout)
|
||||||
except (OSError, asyncio.TimeoutError) as e:
|
except (ConnectionError, asyncio.TimeoutError) as e:
|
||||||
__log__.warning('Attempt {} at connecting failed: {}: {}'
|
__log__.warning('Attempt {} at connecting failed: {}: {}'
|
||||||
.format(retry, type(e).__name__, e))
|
.format(retry, type(e).__name__, e))
|
||||||
else:
|
else:
|
||||||
|
@ -287,9 +287,6 @@ class MTProtoSender:
|
||||||
__log__.debug('Awaiting for the receive loop before reconnecting...')
|
__log__.debug('Awaiting for the receive loop before reconnecting...')
|
||||||
await self._recv_loop_handle
|
await self._recv_loop_handle
|
||||||
|
|
||||||
__log__.debug('Closing current connection...')
|
|
||||||
self._connection.disconnect()
|
|
||||||
|
|
||||||
self._reconnecting = False
|
self._reconnecting = False
|
||||||
|
|
||||||
# Start with a clean state (and thus session ID) to avoid old msgs
|
# Start with a clean state (and thus session ID) to avoid old msgs
|
||||||
|
@ -334,19 +331,31 @@ class MTProtoSender:
|
||||||
self._last_acks.append(ack)
|
self._last_acks.append(ack)
|
||||||
self._pending_ack.clear()
|
self._pending_ack.clear()
|
||||||
|
|
||||||
batch, data = await self._send_queue.get(
|
__log__.debug('Waiting for messages to send...')
|
||||||
self._connection.disconnected)
|
# TODO Wait for the connection send queue to be empty?
|
||||||
|
# This means that while it's not empty we can wait for
|
||||||
|
# more messages to be added to the send queue.
|
||||||
|
try:
|
||||||
|
batch, data = await self._send_queue.get(
|
||||||
|
self._connection.disconnected)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
return
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
__log__.debug('Encrypting %d message(s) in %d bytes for sending',
|
||||||
|
len(batch), len(data))
|
||||||
|
|
||||||
|
data = self._state.encrypt_message_data(data)
|
||||||
try:
|
try:
|
||||||
# TODO Split except
|
|
||||||
data = self._state.encrypt_message_data(data)
|
|
||||||
await self._connection.send(data)
|
await self._connection.send(data)
|
||||||
except Exception:
|
except asyncio.CancelledError:
|
||||||
__log__.exception('Unhandled error while sending data')
|
return
|
||||||
continue
|
except ConnectionError:
|
||||||
|
__log__.info('Connection closed while sending data')
|
||||||
|
self._start_reconnect()
|
||||||
|
return
|
||||||
|
|
||||||
for state in batch:
|
for state in batch:
|
||||||
if not isinstance(state, list):
|
if not isinstance(state, list):
|
||||||
|
@ -357,6 +366,8 @@ class MTProtoSender:
|
||||||
if isinstance(s.request, TLRequest):
|
if isinstance(s.request, TLRequest):
|
||||||
self._pending_state[s.msg_id] = s
|
self._pending_state[s.msg_id] = s
|
||||||
|
|
||||||
|
__log__.debug('Encrypted messages put in a queue to be sent')
|
||||||
|
|
||||||
async def _recv_loop(self):
|
async def _recv_loop(self):
|
||||||
"""
|
"""
|
||||||
This loop is responsible for reading all incoming responses
|
This loop is responsible for reading all incoming responses
|
||||||
|
@ -367,10 +378,18 @@ class MTProtoSender:
|
||||||
while self._user_connected and not self._reconnecting:
|
while self._user_connected and not self._reconnecting:
|
||||||
__log__.debug('Receiving items from the network...')
|
__log__.debug('Receiving items from the network...')
|
||||||
try:
|
try:
|
||||||
# TODO Split except
|
|
||||||
body = await self._connection.recv()
|
body = await self._connection.recv()
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
return
|
||||||
|
except ConnectionError:
|
||||||
|
__log__.info('Connection closed while receiving data')
|
||||||
|
self._start_reconnect()
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
message = self._state.decrypt_message_data(body)
|
message = self._state.decrypt_message_data(body)
|
||||||
except TypeNotFoundError as e:
|
except TypeNotFoundError as e:
|
||||||
|
# Received object which we don't know how to deserialize
|
||||||
__log__.info('Type %08x not found, remaining data %r',
|
__log__.info('Type %08x not found, remaining data %r',
|
||||||
e.invalid_constructor_id, e.remaining)
|
e.invalid_constructor_id, e.remaining)
|
||||||
continue
|
continue
|
||||||
|
@ -380,13 +399,6 @@ class MTProtoSender:
|
||||||
__log__.warning('Security error while unpacking a '
|
__log__.warning('Security error while unpacking a '
|
||||||
'received message: %s', e)
|
'received message: %s', e)
|
||||||
continue
|
continue
|
||||||
except InvalidChecksumError as e:
|
|
||||||
__log__.warning(
|
|
||||||
'Invalid checksum on the read packet (was %s expected %s)',
|
|
||||||
e.checksum, e.valid_checksum
|
|
||||||
)
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
return
|
|
||||||
except BufferError as e:
|
except BufferError as e:
|
||||||
if isinstance(e, InvalidBufferError) and e.code == 404:
|
if isinstance(e, InvalidBufferError) and e.code == 404:
|
||||||
__log__.info('Broken authorization key; resetting')
|
__log__.info('Broken authorization key; resetting')
|
||||||
|
@ -396,19 +408,15 @@ class MTProtoSender:
|
||||||
self._auth_key.key = None
|
self._auth_key.key = None
|
||||||
self._start_reconnect()
|
self._start_reconnect()
|
||||||
return
|
return
|
||||||
except asyncio.IncompleteReadError:
|
|
||||||
__log__.info('Telegram closed the connection')
|
|
||||||
self._start_reconnect()
|
|
||||||
return
|
|
||||||
except Exception:
|
except Exception:
|
||||||
__log__.exception('Unhandled error while receiving data')
|
__log__.exception('Unhandled error while receiving data')
|
||||||
self._start_reconnect()
|
self._start_reconnect()
|
||||||
return
|
return
|
||||||
else:
|
|
||||||
try:
|
try:
|
||||||
await self._process_message(message)
|
await self._process_message(message)
|
||||||
except Exception:
|
except Exception:
|
||||||
__log__.exception('Unhandled error while processing msgs')
|
__log__.exception('Unhandled error while processing msgs')
|
||||||
|
|
||||||
# Response Handlers
|
# Response Handlers
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user