From fa467c0281cc39d7d464cadd72df5ee92d04f8a2 Mon Sep 17 00:00:00 2001 From: xairaven Date: Fri, 19 Jul 2024 21:25:30 +0300 Subject: [PATCH] (Feat) Replaced all error messages to localization file --- media_fetch_bot/locales/en.json | 22 ++++++++++++++++++- media_fetch_bot/src/general_errors/env.rs | 10 ++++----- .../src/general_errors/user_input.rs | 4 ++-- media_fetch_bot/src/main.rs | 9 ++++---- .../src/tiktok/errors/api_error.rs | 12 +++++----- .../src/tiktok/errors/error_type.rs | 4 ++-- .../src/tiktok/errors/user_error.rs | 2 +- 7 files changed, 41 insertions(+), 22 deletions(-) diff --git a/media_fetch_bot/locales/en.json b/media_fetch_bot/locales/en.json index c34e940..bfab83f 100644 --- a/media_fetch_bot/locales/en.json +++ b/media_fetch_bot/locales/en.json @@ -2,5 +2,25 @@ "_version": 1, "Help": "Welcome to the Media Downloader Bot!\n\nCommands:\n- /help: Display this help message.\n\nHow to use:\nTo download media from TikTok simply send a TikTok video or photo slides link directly to the bot.\nThe bot will process the link and send the media file back to you.\n\nAdditional Notes:\nEnsure the TikTok link is directly accessible (not private or restricted).\n\nSupported formats: Video and photo slides.", - "TikTokBackendErrorMessage": "Error message:\nSomething is wrong either with the bot or with TikTok API.\nPlease report this incident to the bot administrator." + + "ApiKeyTiktokMissing": "Tiktok API Key missing. Tiktok module is not working.", + "FailedGetResponse": "Failed to get a response from API.", + "FailedParseResponse": "Failed to parse response.", + "FailedParseUrl": "Failed to parse final url.", + "WrongApiHost": "x-rapidapi-host header is wrong.", + "WrongApiKey": "x-rapidapi-key header is wrong.", + + "ConfigNotLoaded": "Something wrong with .env file. May be, it not exists.", + "LogLevelNotLoaded": "Something wrong with .env LOG_LEVEL. May be, there's no field with this name.", + "LogLevelUndefined": "Undefined LOG_LEVEL. Possible levels: error, warn, info, debug, trace, off", + "NameNotLoaded": "Something wrong with .env BOT_NAME. May be, there's no field with this name.", + "TokenNotLoaded": "Something wrong with .env BOT_TOKEN. May be, there's no field with this name.", + + "EmptyMessage": "Empty message.", + "LinkTypeUndefined": "Link type undefined.", + + "NoResult": "Unfortunately, there's no content at this link.", + + "TikTokBackendError": "Error message:\nSomething is wrong either with the bot or with TikTok API.\nPlease report this incident to the bot administrator.", + "TikTokUserError": "User Error. Something wrong with user input" } \ No newline at end of file diff --git a/media_fetch_bot/src/general_errors/env.rs b/media_fetch_bot/src/general_errors/env.rs index 5cd8867..5c96fc1 100644 --- a/media_fetch_bot/src/general_errors/env.rs +++ b/media_fetch_bot/src/general_errors/env.rs @@ -2,18 +2,18 @@ use thiserror::Error; #[derive(Debug, Error)] pub enum EnvError { - #[error("ConfigNotLoaded: Something wrong with .env file. May be, it not exists.")] + #[error("ConfigNotLoaded")] ConfigNotLoaded, - #[error("LogLevelNotLoaded: Something wrong with .env LOG_LEVEL. May be, there's no field with this name.")] + #[error("LogLevelNotLoaded")] LogLevelNotLoaded, - #[error("LogLevelUndefined: Undefined LOG_LEVEL. Possible levels: error, warn, info, debug, trace, off")] + #[error("LogLevelUndefined")] LogLevelUndefined, - #[error("NameNotLoaded: Something wrong with .env BOT_NAME. May be, there's no field with this name.")] + #[error("NameNotLoaded")] NameNotLoaded, - #[error("TokenNotLoaded: Something wrong with .env BOT_TOKEN. May be, there's no field with this name.")] + #[error("TokenNotLoaded")] TokenNotLoaded, } \ No newline at end of file diff --git a/media_fetch_bot/src/general_errors/user_input.rs b/media_fetch_bot/src/general_errors/user_input.rs index 9b744d9..43ea834 100644 --- a/media_fetch_bot/src/general_errors/user_input.rs +++ b/media_fetch_bot/src/general_errors/user_input.rs @@ -2,9 +2,9 @@ use thiserror::Error; #[derive(Debug, Error)] pub enum UserInputError { - #[error("Empty message.")] + #[error("EmptyMessage")] EmptyMessage, - #[error("Link type undefined.")] + #[error("LinkTypeUndefined")] LinkTypeUndefined } \ No newline at end of file diff --git a/media_fetch_bot/src/main.rs b/media_fetch_bot/src/main.rs index e7a55c1..6a79284 100644 --- a/media_fetch_bot/src/main.rs +++ b/media_fetch_bot/src/main.rs @@ -50,8 +50,7 @@ async fn handle_message(bot: Bot, msg: Message, tiktok_api_key: Option) -> ResponseResult<()> { let text = match msg.text() { None => { - bot.send_message(msg.chat.id, - UserInputError::EmptyMessage.to_string()).await?; + bot.send_message(msg.chat.id, t!(&UserInputError::EmptyMessage.to_string())).await?; return Ok(()); } Some(value) => value @@ -68,7 +67,7 @@ async fn handle_message(bot: Bot, msg: Message, } _ => { bot.send_message(msg.chat.id, - UserInputError::LinkTypeUndefined.to_string()).await?; + t!(&UserInputError::LinkTypeUndefined.to_string())).await?; log::info!("{}", format!("ChatID: {} -> Undefined: {}", msg.chat.id, text)); } } @@ -116,7 +115,7 @@ async fn handle_tiktok_link(link: &str, api_key: Option, Err(err) => { let error_text = match err { ErrorType::Backend(ref specific_err) => { - log::error!("{}", format!("{}: ChatID: {} -> ErrQuery: {}", + log::error!("{}", format!("{}. ChatID: {} -> ErrQuery: {}", specific_err, msg.chat.id, link)); format!("{}", t!(&err.to_string())) @@ -125,7 +124,7 @@ async fn handle_tiktok_link(link: &str, api_key: Option, log::warn!("{}", format!("ChatID: {} -> ErrQuery: {}", msg.chat.id, link)); - format!("{}", err) + format!("{}", t!(&err.to_string())) } }; diff --git a/media_fetch_bot/src/tiktok/errors/api_error.rs b/media_fetch_bot/src/tiktok/errors/api_error.rs index 67d6241..b0ff4fb 100644 --- a/media_fetch_bot/src/tiktok/errors/api_error.rs +++ b/media_fetch_bot/src/tiktok/errors/api_error.rs @@ -2,21 +2,21 @@ use thiserror::Error; #[derive(Debug, Error)] pub enum ApiError { - #[error("Tiktok API Key missing. Tiktok module is not working.")] + #[error("ApiKeyTiktokMissing")] ApiKeyTiktokMissing, - #[error("FailedGetResponse: Failed to get a response from API.")] + #[error("FailedGetResponse")] FailedGetResponse, - #[error("FailedParseResponse: Failed to parse response.")] + #[error("FailedParseResponse")] FailedParseResponse, - #[error("FailedParseUrl: Failed to parse final url.")] + #[error("FailedParseUrl")] FailedParseUrl, - #[error("WrongApiHost: x-rapidapi-host header is wrong.")] + #[error("WrongApiHost")] WrongApiHost, - #[error("WrongApiKey: x-rapidapi-key header is wrong.")] + #[error("WrongApiKey")] WrongApiKey, } \ No newline at end of file diff --git a/media_fetch_bot/src/tiktok/errors/error_type.rs b/media_fetch_bot/src/tiktok/errors/error_type.rs index a27636d..ad48cc6 100644 --- a/media_fetch_bot/src/tiktok/errors/error_type.rs +++ b/media_fetch_bot/src/tiktok/errors/error_type.rs @@ -4,9 +4,9 @@ use crate::tiktok::errors::user_error::UserError; #[derive(Error, Debug)] pub enum ErrorType { - #[error("TikTokBackendErrorMessage")] + #[error("TikTokBackendError")] Backend(#[from] ApiError), - #[error("User Error. Something wrong with user input")] + #[error("TikTokUserError")] User(#[from] UserError) } \ No newline at end of file diff --git a/media_fetch_bot/src/tiktok/errors/user_error.rs b/media_fetch_bot/src/tiktok/errors/user_error.rs index 422b51c..9abb7b9 100644 --- a/media_fetch_bot/src/tiktok/errors/user_error.rs +++ b/media_fetch_bot/src/tiktok/errors/user_error.rs @@ -2,6 +2,6 @@ use thiserror::Error; #[derive(Debug, Error)] pub enum UserError { - #[error("Unfortunately, there's no content by this link.")] + #[error("NoResult")] NoResult, } \ No newline at end of file