2018-11-30 23:39:13 +03:00
|
|
|
import csv
|
2018-12-03 13:08:20 +03:00
|
|
|
import re
|
2018-04-14 14:56:39 +03:00
|
|
|
|
2021-09-12 17:23:55 +03:00
|
|
|
from ..utils import snake_to_camel_case
|
2018-04-14 14:56:39 +03:00
|
|
|
|
|
|
|
# Core base classes depending on the integer error code
|
|
|
|
KNOWN_BASE_CLASSES = {
|
|
|
|
303: 'InvalidDCError',
|
|
|
|
400: 'BadRequestError',
|
|
|
|
401: 'UnauthorizedError',
|
|
|
|
403: 'ForbiddenError',
|
|
|
|
404: 'NotFoundError',
|
|
|
|
406: 'AuthKeyError',
|
|
|
|
420: 'FloodError',
|
|
|
|
500: 'ServerError',
|
2019-05-05 20:57:09 +03:00
|
|
|
503: 'TimedOutError'
|
2018-04-14 14:56:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-24 21:07:34 +03:00
|
|
|
def _get_canonical_name(error_code):
|
2018-04-14 14:56:39 +03:00
|
|
|
"""
|
2021-09-24 21:07:34 +03:00
|
|
|
Gets the corresponding canonical name for the given error code.
|
2018-04-14 14:56:39 +03:00
|
|
|
"""
|
2021-09-24 21:07:34 +03:00
|
|
|
# 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')]
|
2018-04-14 14:56:39 +03:00
|
|
|
|
2021-09-24 21:07:34 +03:00
|
|
|
return name
|
2018-04-14 14:56:39 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Error:
|
2018-11-30 23:39:13 +03:00
|
|
|
def __init__(self, codes, name, description):
|
|
|
|
# TODO Some errors have the same name but different integer codes
|
2018-04-14 14:56:39 +03:00
|
|
|
# Should these be split into different files or doesn't really matter?
|
|
|
|
# Telegram isn't exactly consistent with returned errors anyway.
|
2018-11-30 23:39:13 +03:00
|
|
|
self.int_code = codes[0]
|
|
|
|
self.str_code = name
|
2021-09-24 21:07:34 +03:00
|
|
|
self.canonical_name = _get_canonical_name(name)
|
2018-04-14 14:56:39 +03:00
|
|
|
self.description = description
|
|
|
|
|
2021-09-24 21:07:34 +03:00
|
|
|
has_captures = '0' in name
|
|
|
|
if has_captures:
|
2018-12-03 13:08:20 +03:00
|
|
|
self.capture_name = re.search(r'{(\w+)}', description).group(1)
|
2018-04-14 14:56:39 +03:00
|
|
|
else:
|
|
|
|
self.capture_name = None
|
|
|
|
|
|
|
|
|
2018-11-30 23:39:13 +03:00
|
|
|
def parse_errors(csv_file):
|
2018-04-14 14:56:39 +03:00
|
|
|
"""
|
2018-11-30 23:39:13 +03:00
|
|
|
Parses the input CSV file with columns (name, error codes, description)
|
|
|
|
and yields `Error` instances as a result.
|
2018-04-14 14:56:39 +03:00
|
|
|
"""
|
2018-12-21 15:24:16 +03:00
|
|
|
with csv_file.open(newline='') as f:
|
2018-11-30 23:39:13 +03:00
|
|
|
f = csv.reader(f)
|
|
|
|
next(f, None) # header
|
2019-01-24 13:02:58 +03:00
|
|
|
for line, tup in enumerate(f, start=2):
|
|
|
|
try:
|
|
|
|
name, codes, description = tup
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError('Columns count mismatch, unquoted comma in '
|
|
|
|
'desc? (line {})'.format(line)) from None
|
|
|
|
|
2018-11-30 23:39:13 +03:00
|
|
|
try:
|
|
|
|
codes = [int(x) for x in codes.split()] or [400]
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError('Not all codes are integers '
|
|
|
|
'(line {})'.format(line)) from None
|
|
|
|
|
|
|
|
yield Error([int(x) for x in codes], name, description)
|