mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-06-02 20:53:09 +03:00
Add option to raise last error instead of generic ValueError (#1571)
This commit is contained in:
parent
d5e4398ace
commit
8ce7e776c1
|
@ -160,6 +160,12 @@ class TelegramBaseClient(abc.ABC):
|
||||||
was for 21s, it would ``raise FloodWaitError`` instead. Values
|
was for 21s, it would ``raise FloodWaitError`` instead. Values
|
||||||
larger than a day (like ``float('inf')``) will be changed to a day.
|
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 (`str`, optional):
|
||||||
"Device model" to be sent when creating the initial connection.
|
"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.
|
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,
|
auto_reconnect: bool = True,
|
||||||
sequential_updates: bool = False,
|
sequential_updates: bool = False,
|
||||||
flood_sleep_threshold: int = 60,
|
flood_sleep_threshold: int = 60,
|
||||||
|
raise_last_call_error: bool = False,
|
||||||
device_model: str = None,
|
device_model: str = None,
|
||||||
system_version: str = None,
|
system_version: str = None,
|
||||||
app_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._request_retries = request_retries
|
||||||
self._connection_retries = connection_retries
|
self._connection_retries = connection_retries
|
||||||
self._retry_delay = retry_delay or 0
|
self._retry_delay = retry_delay or 0
|
||||||
|
|
|
@ -50,8 +50,9 @@ class UserMethods:
|
||||||
raise errors.FloodWaitError(request=r, capture=diff)
|
raise errors.FloodWaitError(request=r, capture=diff)
|
||||||
|
|
||||||
request_index = 0
|
request_index = 0
|
||||||
|
last_error = None
|
||||||
self._last_request = time.time()
|
self._last_request = time.time()
|
||||||
|
|
||||||
for attempt in retry_range(self._request_retries):
|
for attempt in retry_range(self._request_retries):
|
||||||
try:
|
try:
|
||||||
future = sender.send(request, ordered=ordered)
|
future = sender.send(request, ordered=ordered)
|
||||||
|
@ -82,12 +83,14 @@ class UserMethods:
|
||||||
except (errors.ServerError, errors.RpcCallFailError,
|
except (errors.ServerError, errors.RpcCallFailError,
|
||||||
errors.RpcMcgetFailError, errors.InterdcCallErrorError,
|
errors.RpcMcgetFailError, errors.InterdcCallErrorError,
|
||||||
errors.InterdcCallRichErrorError) as e:
|
errors.InterdcCallRichErrorError) as e:
|
||||||
|
last_error = e
|
||||||
self._log[__name__].warning(
|
self._log[__name__].warning(
|
||||||
'Telegram is having internal issues %s: %s',
|
'Telegram is having internal issues %s: %s',
|
||||||
e.__class__.__name__, e)
|
e.__class__.__name__, e)
|
||||||
|
|
||||||
await asyncio.sleep(2)
|
await asyncio.sleep(2)
|
||||||
except (errors.FloodWaitError, errors.SlowModeWaitError, errors.FloodTestPhoneWaitError) as e:
|
except (errors.FloodWaitError, errors.SlowModeWaitError, errors.FloodTestPhoneWaitError) as e:
|
||||||
|
last_error = e
|
||||||
if utils.is_list_like(request):
|
if utils.is_list_like(request):
|
||||||
request = request[request_index]
|
request = request[request_index]
|
||||||
|
|
||||||
|
@ -106,6 +109,7 @@ class UserMethods:
|
||||||
raise
|
raise
|
||||||
except (errors.PhoneMigrateError, errors.NetworkMigrateError,
|
except (errors.PhoneMigrateError, errors.NetworkMigrateError,
|
||||||
errors.UserMigrateError) as e:
|
errors.UserMigrateError) as e:
|
||||||
|
last_error = e
|
||||||
self._log[__name__].info('Phone migrated to %d', e.new_dc)
|
self._log[__name__].info('Phone migrated to %d', e.new_dc)
|
||||||
should_raise = isinstance(e, (
|
should_raise = isinstance(e, (
|
||||||
errors.PhoneMigrateError, errors.NetworkMigrateError
|
errors.PhoneMigrateError, errors.NetworkMigrateError
|
||||||
|
@ -114,6 +118,8 @@ class UserMethods:
|
||||||
raise
|
raise
|
||||||
await self._switch_dc(e.new_dc)
|
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)'
|
raise ValueError('Request was unsuccessful {} time(s)'
|
||||||
.format(attempt))
|
.format(attempt))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user