mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-09 05:19:41 +00:00
Completely overhaul errors to be generated dynamically
This commit is contained in:
@@ -17,25 +17,16 @@ KNOWN_BASE_CLASSES = {
|
||||
}
|
||||
|
||||
|
||||
def _get_class_name(error_code):
|
||||
def _get_canonical_name(error_code):
|
||||
"""
|
||||
Gets the corresponding class name for the given error code,
|
||||
this either being an integer (thus base error name) or str.
|
||||
Gets the corresponding canonical name for the given error code.
|
||||
"""
|
||||
if isinstance(error_code, int):
|
||||
return KNOWN_BASE_CLASSES.get(
|
||||
abs(error_code), 'RPCError' + str(error_code).replace('-', 'Neg')
|
||||
)
|
||||
# This code should match that of the library itself.
|
||||
name = re.sub(r'[-_\d]', '', error_code).lower()
|
||||
while name.endswith('error'):
|
||||
name = name[:-len('error')]
|
||||
|
||||
if error_code.startswith('2'):
|
||||
error_code = re.sub(r'2', 'TWO_', error_code, count=1)
|
||||
|
||||
if re.match(r'\d+', error_code):
|
||||
raise RuntimeError('error code starting with a digit cannot have valid Python name: {}'.format(error_code))
|
||||
|
||||
return snake_to_camel_case(
|
||||
error_code.replace('FIRSTNAME', 'FIRST_NAME')\
|
||||
.replace('SLOWMODE', 'SLOW_MODE').lower(), suffix='Error')
|
||||
return name
|
||||
|
||||
|
||||
class Error:
|
||||
@@ -45,18 +36,13 @@ class Error:
|
||||
# Telegram isn't exactly consistent with returned errors anyway.
|
||||
self.int_code = codes[0]
|
||||
self.str_code = name
|
||||
self.subclass = _get_class_name(codes[0])
|
||||
self.subclass_exists = abs(codes[0]) in KNOWN_BASE_CLASSES
|
||||
self.canonical_name = _get_canonical_name(name)
|
||||
self.description = description
|
||||
|
||||
self.has_captures = '_X' in name
|
||||
if self.has_captures:
|
||||
self.name = _get_class_name(name.replace('_X', '_'))
|
||||
self.pattern = name.replace('_X', r'_(\d+)')
|
||||
has_captures = '0' in name
|
||||
if has_captures:
|
||||
self.capture_name = re.search(r'{(\w+)}', description).group(1)
|
||||
else:
|
||||
self.name = _get_class_name(name)
|
||||
self.pattern = name
|
||||
self.capture_name = None
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user