From 01848de7e276351d89eaba7e7f83a6a7d755c1a9 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 3 Dec 2018 11:08:20 +0100 Subject: [PATCH] Move error capture names to errors.csv --- telethon_generator/data/errors.csv | 30 +++++++++---------------- telethon_generator/generators/errors.py | 2 +- telethon_generator/parsers/errors.py | 16 ++----------- 3 files changed, 14 insertions(+), 34 deletions(-) diff --git a/telethon_generator/data/errors.csv b/telethon_generator/data/errors.csv index ae94ac08..cd2d9195 100644 --- a/telethon_generator/data/errors.csv +++ b/telethon_generator/data/errors.csv @@ -18,7 +18,7 @@ BOT_CHANNELS_NA,400,Bots can't edit admin privileges BOT_GROUPS_BLOCKED,400,This bot can't be added to groups BOT_INLINE_DISABLED,400,This bot can't be used in inline mode BOT_INVALID,400,This is not a valid bot -BOT_METHOD_INVALID,,The API access for bot users is restricted. The method you tried to invoke cannot be executed as a bot +BOT_METHOD_INVALID,400,The API access for bot users is restricted. The method you tried to invoke cannot be executed as a bot BOT_MISSING,400,This method can only be run by a bot BUTTON_DATA_INVALID,400,The provided button data is invalid BUTTON_TYPE_INVALID,400,The type of one of the buttons you provided is invalid @@ -76,17 +76,17 @@ EXTERNAL_URL_INVALID,400,External URL invalid FIELD_NAME_EMPTY,,The field with the name FIELD_NAME is missing FIELD_NAME_INVALID,,The field with the name FIELD_NAME is invalid FILE_ID_INVALID,400,The provided file id is invalid -FILE_MIGRATE_X,303,The file to be accessed is currently stored in DC {} +FILE_MIGRATE_X,303,The file to be accessed is currently stored in DC {new_dc} FILE_PARTS_INVALID,400,The number of file parts is invalid FILE_PART_0_MISSING,,File part 0 missing FILE_PART_EMPTY,400,The provided file part is empty FILE_PART_INVALID,400,The file part number is invalid FILE_PART_LENGTH_INVALID,400,The length of a file part is invalid FILE_PART_SIZE_INVALID,400,The provided file part size is invalid -FILE_PART_X_MISSING,,Part {} of the file is missing from storage +FILE_PART_X_MISSING,400,Part {which} of the file is missing from storage FIRSTNAME_INVALID,400,The first name is invalid -FLOOD_TEST_PHONE_WAIT_X,420,A wait of {} seconds is required in the test servers -FLOOD_WAIT_X,420,A wait of {} seconds is required +FLOOD_TEST_PHONE_WAIT_X,420,A wait of {seconds} seconds is required in the test servers +FLOOD_WAIT_X,420,A wait of {seconds} seconds is required GIF_ID_INVALID,400,The provided GIF ID is invalid GROUPED_MEDIA_INVALID,400,Invalid grouped media HASH_INVALID,400,The provided hash is invalid @@ -100,16 +100,8 @@ INPUT_LAYER_INVALID,400,The provided layer is invalid INPUT_METHOD_INVALID,,The invoked method does not exist anymore or has never existed INPUT_REQUEST_TOO_LONG,400,The input request was too long. This may be a bug in the library as it can occur when serializing more bytes than it should (like appending the vector constructor code at the end of a message) INPUT_USER_DEACTIVATED,400,The specified user was deleted -INTERDC_1_CALL_ERROR,,An error occurred while communicating with DC 1 -INTERDC_1_CALL_RICH_ERROR,,A rich error occurred while communicating with DC 1 -INTERDC_2_CALL_ERROR,,An error occurred while communicating with DC 2 -INTERDC_2_CALL_RICH_ERROR,,A rich error occurred while communicating with DC 2 -INTERDC_3_CALL_ERROR,,An error occurred while communicating with DC 3 -INTERDC_3_CALL_RICH_ERROR,,A rich error occurred while communicating with DC 3 -INTERDC_4_CALL_ERROR,,An error occurred while communicating with DC 4 -INTERDC_4_CALL_RICH_ERROR,,A rich error occurred while communicating with DC 4 -INTERDC_5_CALL_ERROR,,An error occurred while communicating with DC 5 -INTERDC_5_CALL_RICH_ERROR,,A rich error occurred while communicating with DC 5 +INTERDC_X_CALL_ERROR,,An error occurred while communicating with DC {dc} +INTERDC_X_CALL_RICH_ERROR,,A rich error occurred while communicating with DC {dc} INVITE_HASH_EMPTY,400,The invite hash is empty INVITE_HASH_EXPIRED,400,The chat the user tried to join has expired and is not valid anymore INVITE_HASH_INVALID,400,The invite hash is invalid @@ -137,7 +129,7 @@ MESSAGE_TOO_LONG,400,Message was too long. Current maximum length is 4096 UTF-8 MSG_WAIT_FAILED,400,A waiting call returned an error NEED_CHAT_INVALID,500,The provided chat is invalid NEED_MEMBER_INVALID,500,The provided member is invalid -NETWORK_MIGRATE_X,303,The source IP address is associated with DC {} +NETWORK_MIGRATE_X,303,The source IP address is associated with DC {new_dc} NEW_SALT_INVALID,400,The new salt is invalid NEW_SETTINGS_INVALID,400,The new settings are invalid OFFSET_INVALID,400,"The given offset was invalid, it must be divisible by 1KB. See https://core.telegram.org/api/files#downloading-files" @@ -159,7 +151,7 @@ PHONE_CODE_EMPTY,400,The phone code is missing PHONE_CODE_EXPIRED,400,The confirmation code has expired PHONE_CODE_HASH_EMPTY,,The phone code hash is missing PHONE_CODE_INVALID,400,The phone code entered was invalid -PHONE_MIGRATE_X,303,The phone number a user is trying to use for authorization is associated with DC {} +PHONE_MIGRATE_X,303,The phone number a user is trying to use for authorization is associated with DC {new_dc} PHONE_NUMBER_APP_SIGNUP_FORBIDDEN,400, PHONE_NUMBER_BANNED,400,The used phone number has been banned from Telegram and cannot be used anymore. Maybe check https://www.telegram.org/faq_spam PHONE_NUMBER_FLOOD,400,You asked for the code too many times. @@ -208,7 +200,7 @@ STICKER_INVALID,400,The provided sticker is invalid STICKER_PNG_DIMENSIONS,400,Sticker png dimensions invalid STORAGE_CHECK_FAILED,500,Server storage check failed STORE_INVALID_SCALAR_TYPE,500, -TAKEOUT_INIT_DELAY_X,420,A wait of {} seconds is required before being able to initiate the takeout +TAKEOUT_INIT_DELAY_X,420,A wait of {seconds} seconds is required before being able to initiate the takeout TAKEOUT_REQUIRED,400,You must initialize a takeout request first TEMP_AUTH_KEY_EMPTY,400,No temporary auth key provided Timeout,-503,A timeout occurred while fetching data from the bot @@ -239,7 +231,7 @@ USER_ID_INVALID,400,"Invalid object ID for a user. Make sure to pass the right t USER_IS_BLOCKED,400 403,User is blocked USER_IS_BOT,400,Bots can't send messages to other bots USER_KICKED,400,This user was kicked from this supergroup/channel -USER_MIGRATE_X,303,The user whose identity is being used to execute queries is associated with DC {} +USER_MIGRATE_X,303,The user whose identity is being used to execute queries is associated with DC {new_dc} USER_NOT_MUTUAL_CONTACT,400 403,The provided user is not a mutual contact USER_NOT_PARTICIPANT,400,The target user is not a member of the specified megagroup or channel USER_PRIVACY_RESTRICTED,403,The user's privacy settings do not allow you to do this diff --git a/telethon_generator/generators/errors.py b/telethon_generator/generators/errors.py index 3f38c14a..f373a5a1 100644 --- a/telethon_generator/generators/errors.py +++ b/telethon_generator/generators/errors.py @@ -38,7 +38,7 @@ def generate_errors(errors, f): '{}'.format(repr(error.description))) if error.has_captures: - f.write('.format(self.{})'.format(error.capture_name)) + f.write('.format({0}=self.{0})'.format(error.capture_name)) f.write(' + self._fmt_request(request))\n') diff --git a/telethon_generator/parsers/errors.py b/telethon_generator/parsers/errors.py index f4d99dc2..ed32531f 100644 --- a/telethon_generator/parsers/errors.py +++ b/telethon_generator/parsers/errors.py @@ -1,4 +1,5 @@ import csv +import re from ..utils import snake_to_camel_case @@ -14,19 +15,6 @@ KNOWN_BASE_CLASSES = { 500: 'ServerError', } -# Give better semantic names to some captures -# TODO Move this to the CSV? -CAPTURE_NAMES = { - 'FloodWaitError': 'seconds', - 'FloodTestPhoneWaitError': 'seconds', - 'TakeoutInitDelayError': 'seconds', - 'FileMigrateError': 'new_dc', - 'NetworkMigrateError': 'new_dc', - 'PhoneMigrateError': 'new_dc', - 'UserMigrateError': 'new_dc', - 'FilePartMissingError': 'which' -} - def _get_class_name(error_code): """ @@ -57,7 +45,7 @@ class Error: if self.has_captures: self.name = _get_class_name(name.replace('_X', '')) self.pattern = name.replace('_X', r'_(\d+)') - self.capture_name = CAPTURE_NAMES.get(self.name, 'x') + self.capture_name = re.search(r'{(\w+)}', description).group(1) else: self.name = _get_class_name(name) self.pattern = name