2017-11-26 19:06:09 +03:00
|
|
|
"""
|
|
|
|
This module holds all the base and automatically generated errors that the
|
|
|
|
Telegram API has. See telethon_generator/errors.json for more.
|
|
|
|
"""
|
2017-06-10 12:47:51 +03:00
|
|
|
import re
|
|
|
|
|
|
|
|
from .common import (
|
2017-12-28 02:22:28 +03:00
|
|
|
ReadCancelledError, TypeNotFoundError, InvalidChecksumError,
|
2018-10-12 23:17:07 +03:00
|
|
|
InvalidBufferError, SecurityError, CdnFileTamperedError,
|
2019-05-15 14:57:55 +03:00
|
|
|
AlreadyInConversationError, BadMessageError, MultiError
|
2017-06-10 12:47:51 +03:00
|
|
|
)
|
|
|
|
|
2017-10-20 16:44:43 +03:00
|
|
|
# This imports the base errors too, as they're imported there
|
2018-06-18 22:02:42 +03:00
|
|
|
from .rpcbaseerrors import *
|
|
|
|
from .rpcerrorlist import *
|
2017-06-10 12:47:51 +03:00
|
|
|
|
|
|
|
|
2018-10-15 19:35:51 +03:00
|
|
|
def rpc_message_to_error(rpc_error, request):
|
2017-11-26 19:06:09 +03:00
|
|
|
"""
|
|
|
|
Converts a Telegram's RPC Error to a Python error.
|
|
|
|
|
2018-06-09 14:11:49 +03:00
|
|
|
:param rpc_error: the RpcError instance.
|
2018-10-15 19:35:51 +03:00
|
|
|
:param request: the request that caused this error.
|
2017-11-26 19:06:09 +03:00
|
|
|
:return: the RPCError as a Python exception that represents this error.
|
|
|
|
"""
|
2017-10-20 16:44:43 +03:00
|
|
|
# Try to get the error by direct look-up, otherwise regex
|
2021-01-29 22:19:07 +03:00
|
|
|
# Case-insensitive, for things like "timeout" which don't conform.
|
|
|
|
cls = rpc_errors_dict.get(rpc_error.error_message.upper(), None)
|
2017-10-20 16:44:43 +03:00
|
|
|
if cls:
|
2019-05-03 14:59:17 +03:00
|
|
|
return cls(request=request)
|
2017-10-20 16:44:43 +03:00
|
|
|
|
2018-06-18 20:09:45 +03:00
|
|
|
for msg_regex, cls in rpc_errors_re:
|
2018-06-09 14:11:49 +03:00
|
|
|
m = re.match(msg_regex, rpc_error.error_message)
|
2017-10-20 16:44:43 +03:00
|
|
|
if m:
|
|
|
|
capture = int(m.group(1)) if m.groups() else None
|
2019-05-03 14:59:17 +03:00
|
|
|
return cls(request=request, capture=capture)
|
2017-10-20 16:44:43 +03:00
|
|
|
|
2019-04-23 12:28:09 +03:00
|
|
|
# Some errors are negative:
|
|
|
|
# * -500 for "No workers running",
|
|
|
|
# * -503 for "Timeout"
|
|
|
|
#
|
|
|
|
# We treat them as if they were positive, so -500 will be treated
|
|
|
|
# as a `ServerError`, etc.
|
2019-05-03 14:59:17 +03:00
|
|
|
cls = base_errors.get(abs(rpc_error.error_code), RPCError)
|
|
|
|
return cls(request=request, message=rpc_error.error_message,
|
|
|
|
code=rpc_error.error_code)
|