refactor: refactor string format for logging and exceptions

This commit is contained in:
parent 09667e29f9
commit 942abb2375
2 changed files with 15 additions and 15 deletions

View File

@ -77,7 +77,7 @@ class TranslateCommandHandler(AbstractCommandHandler):
"""
self.logger: Logger = logger
self.translate_client: GoogleTranslateClient = translate_client
self.logger.info("TranslateCommandHandler initialized successfully.")
self.logger.info("GoogleTranslateClient initialized successfully.")
@property
def COMMAND(self) -> str:
@ -106,7 +106,7 @@ class TranslateCommandHandler(AbstractCommandHandler):
message (`pyrogram.types.Message`): The incoming message object to process.
"""
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
@ -125,14 +125,15 @@ class TranslateCommandHandler(AbstractCommandHandler):
destination_language = match_dest.group(1)
# 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:
text = text_parts
self.logger.debug(
"Parsed parameters - source_language: %s, destination_language: %s, text length: %s",
source_language, destination_language, len(text) if text else 0
f"Parsed parameters - source_language: {source_language}, destination_language: {destination_language}, text length: {len(text) if text else 0}"
)
# Resolve language aliases
@ -155,7 +156,7 @@ class TranslateCommandHandler(AbstractCommandHandler):
quote=True
)
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
@ -186,8 +187,7 @@ class TranslateCommandHandler(AbstractCommandHandler):
except Exception as error:
self.logger.error(
"Error processing /%s command for chat_id=%s: %s",
self.COMMAND, message.chat.id, error,
f"Error processing /{self.COMMAND} command for chat_id={message.chat.id}: {error}",
exc_info=True
)
await processing_message.edit_text(
@ -213,5 +213,5 @@ class TranslateCommandHandler(AbstractCommandHandler):
for language_code, aliases in self.LANGUAGE_ALIASES.items():
if normalized_input in aliases:
return language_code
self.logger.warning("Invalid language parameter provided: %s", language_input)
raise ValueError("Invalid language parameter provided: %s", language_input)
self.logger.warning(f"Invalid language parameter provided: {language_input}")
raise ValueError(f"Invalid language parameter provided: {language_input}")

View File

@ -46,7 +46,7 @@ class GoogleTranslateClient:
self.logger.debug("Detection language completed successfully.")
return detection
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}")
async def translate_text(
@ -67,14 +67,14 @@ class GoogleTranslateClient:
`googletrans.models.Translated`: The translation object containing the translated text and metadata.
"""
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(
text, dest_lang, src_lang
)
self.logger.info("Translation completed successfully.")
return translation
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}")
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.
"""
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(
texts, dest_lang, src_lang
)
self.logger.info("Batch translation completed successfully.")
return translations
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}")