Telethon/telethon/errors/__init__.py

42 lines
1.3 KiB
Python
Raw Normal View History

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.
"""
import re
from .common import (
ReadCancelledError, TypeNotFoundError, InvalidChecksumError,
InvalidBufferError, SecurityError, CdnFileTamperedError,
AlreadyInConversationError, MultiError
)
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 *
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.
:param rpc_error: the RpcError instance.
: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
cls = rpc_errors_dict.get(rpc_error.error_message, None)
2017-10-20 16:44:43 +03:00
if cls:
return cls(request)
2017-10-20 16:44:43 +03:00
for msg_regex, cls in rpc_errors_re:
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
return cls(request, capture=capture)
2017-10-20 16:44:43 +03:00
cls = base_errors.get(rpc_error.error_code)
if cls:
return cls(request, rpc_error.error_message)
return RPCError(request, rpc_error.error_message, rpc_error.error_code)