Skip to content

Commit

Permalink
(Feat) Replaced all error messages to localization file
Browse files Browse the repository at this point in the history
  • Loading branch information
xairaven committed Jul 19, 2024
1 parent cc54bec commit fa467c0
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 22 deletions.
22 changes: 21 additions & 1 deletion media_fetch_bot/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,25 @@
"_version": 1,
"Help": "<b>Welcome to the Media Downloader Bot!</b>\n\n<b>Commands:</b>\n- /help: Display this help message.\n\n<b>How to use:</b>\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\n<b>Additional Notes:</b>\nEnsure the TikTok link is directly accessible (not private or restricted).\n\n<b>Supported formats:</b> Video and photo slides.",

"TikTokBackendErrorMessage": "<b>Error message:</b>\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": "<b>Error message:</b>\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"
}
10 changes: 5 additions & 5 deletions media_fetch_bot/src/general_errors/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
4 changes: 2 additions & 2 deletions media_fetch_bot/src/general_errors/user_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 4 additions & 5 deletions media_fetch_bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ async fn handle_message(bot: Bot, msg: Message,
tiktok_api_key: Option<String>) -> 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
Expand All @@ -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));
}
}
Expand Down Expand Up @@ -116,7 +115,7 @@ async fn handle_tiktok_link(link: &str, api_key: Option<String>,
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()))
Expand All @@ -125,7 +124,7 @@ async fn handle_tiktok_link(link: &str, api_key: Option<String>,
log::warn!("{}", format!("ChatID: {} -> ErrQuery: {}",
msg.chat.id, link));

format!("{}", err)
format!("{}", t!(&err.to_string()))
}
};

Expand Down
12 changes: 6 additions & 6 deletions media_fetch_bot/src/tiktok/errors/api_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
4 changes: 2 additions & 2 deletions media_fetch_bot/src/tiktok/errors/error_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion media_fetch_bot/src/tiktok/errors/user_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit fa467c0

Please sign in to comment.