Add option to raise last error instead of generic ValueError (#1571)

This commit is contained in:
Tulir Asokan 2020-10-01 12:53:17 +03:00 committed by Lonami Exo
parent d5e4398ace
commit 8ce7e776c1
2 changed files with 16 additions and 1 deletions

View File

@ -160,6 +160,12 @@ class TelegramBaseClient(abc.ABC):
was for 21s, it would ``raise FloodWaitError`` instead. Values
larger than a day (like ``float('inf')``) will be changed to a day.
raise_last_call_error (`bool`, optional):
When API calls fail in a way that causes Telethon to retry
automatically, should the RPC error of the last attempt be raised
instead of a generic ValueError. This is mostly useful for
detecting when Telegram has internal issues.
device_model (`str`, optional):
"Device model" to be sent when creating the initial connection.
Defaults to 'PC (n)bit' derived from ``platform.uname().machine``, or its direct value if unknown.
@ -216,6 +222,7 @@ class TelegramBaseClient(abc.ABC):
auto_reconnect: bool = True,
sequential_updates: bool = False,
flood_sleep_threshold: int = 60,
raise_last_call_error: bool = False,
device_model: str = None,
system_version: str = None,
app_version: str = None,
@ -306,6 +313,8 @@ class TelegramBaseClient(abc.ABC):
)
)
self._raise_last_call_error = raise_last_call_error
self._request_retries = request_retries
self._connection_retries = connection_retries
self._retry_delay = retry_delay or 0

View File

@ -50,6 +50,7 @@ class UserMethods:
raise errors.FloodWaitError(request=r, capture=diff)
request_index = 0
last_error = None
self._last_request = time.time()
for attempt in retry_range(self._request_retries):
@ -82,12 +83,14 @@ class UserMethods:
except (errors.ServerError, errors.RpcCallFailError,
errors.RpcMcgetFailError, errors.InterdcCallErrorError,
errors.InterdcCallRichErrorError) as e:
last_error = e
self._log[__name__].warning(
'Telegram is having internal issues %s: %s',
e.__class__.__name__, e)
await asyncio.sleep(2)
except (errors.FloodWaitError, errors.SlowModeWaitError, errors.FloodTestPhoneWaitError) as e:
last_error = e
if utils.is_list_like(request):
request = request[request_index]
@ -106,6 +109,7 @@ class UserMethods:
raise
except (errors.PhoneMigrateError, errors.NetworkMigrateError,
errors.UserMigrateError) as e:
last_error = e
self._log[__name__].info('Phone migrated to %d', e.new_dc)
should_raise = isinstance(e, (
errors.PhoneMigrateError, errors.NetworkMigrateError
@ -114,6 +118,8 @@ class UserMethods:
raise
await self._switch_dc(e.new_dc)
if self._raise_last_call_error and last_error is not None:
raise last_error
raise ValueError('Request was unsuccessful {} time(s)'
.format(attempt))