refactor: refactor string format for logging and exceptions
This commit is contained in:
parent
09667e29f9
commit
942abb2375
@ -77,7 +77,7 @@ class TranslateCommandHandler(AbstractCommandHandler):
|
|||||||
"""
|
"""
|
||||||
self.logger: Logger = logger
|
self.logger: Logger = logger
|
||||||
self.translate_client: GoogleTranslateClient = translate_client
|
self.translate_client: GoogleTranslateClient = translate_client
|
||||||
self.logger.info("TranslateCommandHandler initialized successfully.")
|
self.logger.info("GoogleTranslateClient initialized successfully.")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def COMMAND(self) -> str:
|
def COMMAND(self) -> str:
|
||||||
@ -106,7 +106,7 @@ class TranslateCommandHandler(AbstractCommandHandler):
|
|||||||
message (`pyrogram.types.Message`): The incoming message object to process.
|
message (`pyrogram.types.Message`): The incoming message object to process.
|
||||||
"""
|
"""
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
"Received /%s command from chat_id=%s.", self.COMMAND, message.chat.id
|
f"Received /{self.COMMAND} command from chat_id={message.chat.id}."
|
||||||
)
|
)
|
||||||
|
|
||||||
# Default values
|
# Default values
|
||||||
@ -125,14 +125,15 @@ class TranslateCommandHandler(AbstractCommandHandler):
|
|||||||
destination_language = match_dest.group(1)
|
destination_language = match_dest.group(1)
|
||||||
|
|
||||||
# Extract text (everything after the last optional parameter)
|
# Extract text (everything after the last optional parameter)
|
||||||
text_parts: str = re.sub(rf"(?:/{self.COMMAND}|src=\w+|source=\w+|dest=\w+|destination=\w+)", "", message.text).strip()
|
text_parts: str = re.sub(
|
||||||
|
rf"(?:/{self.COMMAND}|src=\w+|source=\w+|dest=\w+|destination=\w+)", "", message.text
|
||||||
|
).strip()
|
||||||
|
|
||||||
if text_parts:
|
if text_parts:
|
||||||
text = text_parts
|
text = text_parts
|
||||||
|
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
"Parsed parameters - source_language: %s, destination_language: %s, text length: %s",
|
f"Parsed parameters - source_language: {source_language}, destination_language: {destination_language}, text length: {len(text) if text else 0}"
|
||||||
source_language, destination_language, len(text) if text else 0
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Resolve language aliases
|
# Resolve language aliases
|
||||||
@ -155,7 +156,7 @@ class TranslateCommandHandler(AbstractCommandHandler):
|
|||||||
quote=True
|
quote=True
|
||||||
)
|
)
|
||||||
self.logger.warning(
|
self.logger.warning(
|
||||||
"No argument provided for /%s command in chat_id=%s.", self.COMMAND, message.chat.id
|
f"No argument provided for /{self.COMMAND} command in chat_id={message.chat.id}."
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -186,8 +187,7 @@ class TranslateCommandHandler(AbstractCommandHandler):
|
|||||||
|
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
self.logger.error(
|
self.logger.error(
|
||||||
"Error processing /%s command for chat_id=%s: %s",
|
f"Error processing /{self.COMMAND} command for chat_id={message.chat.id}: {error}",
|
||||||
self.COMMAND, message.chat.id, error,
|
|
||||||
exc_info=True
|
exc_info=True
|
||||||
)
|
)
|
||||||
await processing_message.edit_text(
|
await processing_message.edit_text(
|
||||||
@ -213,5 +213,5 @@ class TranslateCommandHandler(AbstractCommandHandler):
|
|||||||
for language_code, aliases in self.LANGUAGE_ALIASES.items():
|
for language_code, aliases in self.LANGUAGE_ALIASES.items():
|
||||||
if normalized_input in aliases:
|
if normalized_input in aliases:
|
||||||
return language_code
|
return language_code
|
||||||
self.logger.warning("Invalid language parameter provided: %s", language_input)
|
self.logger.warning(f"Invalid language parameter provided: {language_input}")
|
||||||
raise ValueError("Invalid language parameter provided: %s", language_input)
|
raise ValueError(f"Invalid language parameter provided: {language_input}")
|
||||||
|
@ -46,7 +46,7 @@ class GoogleTranslateClient:
|
|||||||
self.logger.debug("Detection language completed successfully.")
|
self.logger.debug("Detection language completed successfully.")
|
||||||
return detection
|
return detection
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error("Error during language detection: %s", e, exc_info=True)
|
self.logger.error(f"Error during language detection: {e}", exc_info=True)
|
||||||
raise RuntimeError(f"Error during language detection: {e}")
|
raise RuntimeError(f"Error during language detection: {e}")
|
||||||
|
|
||||||
async def translate_text(
|
async def translate_text(
|
||||||
@ -67,14 +67,14 @@ class GoogleTranslateClient:
|
|||||||
`googletrans.models.Translated`: The translation object containing the translated text and metadata.
|
`googletrans.models.Translated`: The translation object containing the translated text and metadata.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.logger.info("Translating text to %s from %s.", dest_lang, src_lang)
|
self.logger.info(f"Translating text from {src_lang} to {dest_lang}.")
|
||||||
translation: Translated = await self.translator.translate(
|
translation: Translated = await self.translator.translate(
|
||||||
text, dest_lang, src_lang
|
text, dest_lang, src_lang
|
||||||
)
|
)
|
||||||
self.logger.info("Translation completed successfully.")
|
self.logger.info("Translation completed successfully.")
|
||||||
return translation
|
return translation
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error("Error during translation: %s", e, exc_info=True)
|
self.logger.error(f"Error during translation: {e}", exc_info=True)
|
||||||
raise RuntimeError(f"Error during translation: {e}")
|
raise RuntimeError(f"Error during translation: {e}")
|
||||||
|
|
||||||
async def translate_batch(
|
async def translate_batch(
|
||||||
@ -95,12 +95,12 @@ class GoogleTranslateClient:
|
|||||||
`list[googletrans.models.Translated]`: A list of translation objects containing the translated texts and metadata.
|
`list[googletrans.models.Translated]`: A list of translation objects containing the translated texts and metadata.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.logger.info("Translating batch of %d texts to %s from %s.", len(texts), dest_lang, src_lang)
|
self.logger.info(f"Translating batch of {len(texts)} texts from {src_lang} to {dest_lang}.")
|
||||||
translations: list[Translated] = await self.translator.translate(
|
translations: list[Translated] = await self.translator.translate(
|
||||||
texts, dest_lang, src_lang
|
texts, dest_lang, src_lang
|
||||||
)
|
)
|
||||||
self.logger.info("Batch translation completed successfully.")
|
self.logger.info("Batch translation completed successfully.")
|
||||||
return translations
|
return translations
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error("Error during batch translation: %s", e, exc_info=True)
|
self.logger.error(f"Error during batch translation: {e}", exc_info=True)
|
||||||
raise RuntimeError(f"Error during batch translation: {e}")
|
raise RuntimeError(f"Error during batch translation: {e}")
|
||||||
|
Loading…
Reference in New Issue
Block a user