Moved error handling to one function
This commit is contained in:
parent
ff1d5a039d
commit
4ac34a73bb
@ -10,7 +10,7 @@ crate::export_handlers!(
|
|||||||
change_locale
|
change_locale
|
||||||
);
|
);
|
||||||
|
|
||||||
use crate::{errors::InvalidCommand, locales::LocaleTypeExt};
|
use crate::{errors::InvalidCommand, handle_error, locales::LocaleTypeExt};
|
||||||
use base64::{engine::general_purpose::STANDARD_NO_PAD as B64_ENGINE, Engine as _};
|
use base64::{engine::general_purpose::STANDARD_NO_PAD as B64_ENGINE, Engine as _};
|
||||||
use entity::locale::LocaleType;
|
use entity::locale::LocaleType;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
@ -40,10 +40,7 @@ pub enum CallbackCommand {
|
|||||||
impl CallbackCommand {
|
impl CallbackCommand {
|
||||||
pub fn from_query(q: CallbackQuery) -> Option<Self> {
|
pub fn from_query(q: CallbackQuery) -> Option<Self> {
|
||||||
q.message?;
|
q.message?;
|
||||||
q.data?
|
q.data?.parse().map_err(handle_error).ok()
|
||||||
.parse()
|
|
||||||
.inspect_err(|err| log::error!("{err:?}"))
|
|
||||||
.ok()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ async fn get_master_pass(
|
|||||||
Ok(true) => locale.success.as_str(),
|
Ok(true) => locale.success.as_str(),
|
||||||
Ok(false) => &locale.account_not_found,
|
Ok(false) => &locale.account_not_found,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::error!("{err:?}");
|
handle_error(err);
|
||||||
&locale.something_went_wrong
|
&locale.something_went_wrong
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -15,7 +15,7 @@ pub async fn change_locale(
|
|||||||
let is_successful = new_locale
|
let is_successful = new_locale
|
||||||
.update(user_id, &db)
|
.update(user_id, &db)
|
||||||
.await
|
.await
|
||||||
.inspect_err(|err| log::error!("{err:?}"))
|
.map_err(handle_error)
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
|
|
||||||
if !is_successful {
|
if !is_successful {
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use log::error;
|
|
||||||
|
|
||||||
/// Gets the master password, deletes the accounts and the master password from DB.
|
/// Gets the master password, deletes the accounts and the master password from DB.
|
||||||
/// Although it doesn't use the master password, we get it to be sure that it's the user who used that command
|
/// Although it doesn't use the master password, we get it to be sure that it's the user who used that command
|
||||||
@ -27,8 +26,10 @@ async fn get_master_pass(
|
|||||||
&locale.everything_was_deleted
|
&locale.everything_was_deleted
|
||||||
}
|
}
|
||||||
(Err(err), _) | (_, Err(err)) => {
|
(Err(err), _) | (_, Err(err)) => {
|
||||||
error!("{:?}", crate::Error::from(err));
|
handle_error(err);
|
||||||
txn.rollback().await?;
|
if let Err(err) = txn.rollback().await {
|
||||||
|
handle_error(err);
|
||||||
|
}
|
||||||
&locale.something_went_wrong
|
&locale.something_went_wrong
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
use std::fmt::{Debug, Display};
|
||||||
|
|
||||||
#[derive(thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
#[error("No user info found")]
|
#[error("No user info found")]
|
||||||
pub struct NoUserInfo;
|
pub struct NoUserInfo;
|
||||||
@ -17,3 +19,24 @@ pub enum InvalidCommand {
|
|||||||
#[error("Unknown locale passed into callback")]
|
#[error("Unknown locale passed into callback")]
|
||||||
UnknownLocale,
|
UnknownLocale,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn handle_error<T>(error: T)
|
||||||
|
where
|
||||||
|
T: Debug + Display,
|
||||||
|
{
|
||||||
|
log::error!("{error}\n{error:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ErrorHandler;
|
||||||
|
|
||||||
|
impl teloxide::error_handlers::ErrorHandler<crate::Error> for ErrorHandler {
|
||||||
|
#[inline]
|
||||||
|
fn handle_error(
|
||||||
|
self: std::sync::Arc<Self>,
|
||||||
|
error: crate::Error,
|
||||||
|
) -> futures::prelude::future::BoxFuture<'static, ()> {
|
||||||
|
handle_error(error);
|
||||||
|
Box::pin(async {})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use entity::locale::LocaleType;
|
use entity::locale::LocaleType;
|
||||||
use log::error;
|
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::sync::OnceLock;
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
@ -152,7 +151,7 @@ impl LocaleTypeExt for LocaleType {
|
|||||||
match Self::get_from_db(from.id.0, db).await {
|
match Self::get_from_db(from.id.0, db).await {
|
||||||
Ok(Some(locale)) => return locale,
|
Ok(Some(locale)) => return locale,
|
||||||
Ok(None) => (),
|
Ok(None) => (),
|
||||||
Err(err) => error!("{err:?}"),
|
Err(err) => handle_error(err),
|
||||||
}
|
}
|
||||||
|
|
||||||
from.language_code
|
from.language_code
|
||||||
|
@ -15,7 +15,7 @@ mod state;
|
|||||||
use anyhow::{Error, Result};
|
use anyhow::{Error, Result};
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
use std::env;
|
use std::{env, sync::Arc};
|
||||||
use teloxide::{adaptors::throttle::Limits, dispatching::dialogue::InMemStorage, filter_command};
|
use teloxide::{adaptors::throttle::Limits, dispatching::dialogue::InMemStorage, filter_command};
|
||||||
|
|
||||||
use crate::callbacks::CallbackCommand;
|
use crate::callbacks::CallbackCommand;
|
||||||
@ -80,6 +80,7 @@ fn get_dispatcher(
|
|||||||
|
|
||||||
Dispatcher::builder(bot, handler)
|
Dispatcher::builder(bot, handler)
|
||||||
.dependencies(deps![db, InMemStorage::<State>::new()])
|
.dependencies(deps![db, InMemStorage::<State>::new()])
|
||||||
|
.error_handler(Arc::from(errors::ErrorHandler))
|
||||||
.enable_ctrlc_handler()
|
.enable_ctrlc_handler()
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
pub use crate::{
|
pub use crate::{
|
||||||
commands::Command,
|
commands::Command,
|
||||||
errors::*,
|
errors::{handle_error, NoUserInfo},
|
||||||
first_handler, handler,
|
first_handler, handler,
|
||||||
locales::{Locale, LocaleRef},
|
locales::{Locale, LocaleRef},
|
||||||
markups::*,
|
markups::*,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::prelude::*;
|
use crate::{errors::HandlerUsed, prelude::*};
|
||||||
use futures::future::BoxFuture;
|
use futures::future::BoxFuture;
|
||||||
|
|
||||||
/// A generic state handler. It checks for "/cancel" messages and runs the provided validation function
|
/// A generic state handler. It checks for "/cancel" messages and runs the provided validation function
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use cryptography::hashing::HashedBytes;
|
use cryptography::hashing::HashedBytes;
|
||||||
use log::error;
|
|
||||||
use tokio::task::spawn_blocking;
|
use tokio::task::spawn_blocking;
|
||||||
|
|
||||||
/// Returns true if the provided master password is valid
|
/// Returns true if the provided master password is valid
|
||||||
@ -13,12 +12,9 @@ async fn check_master_pass(
|
|||||||
) -> crate::Result<Option<String>> {
|
) -> crate::Result<Option<String>> {
|
||||||
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
|
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
|
||||||
let Some(model) = MasterPass::get(user_id, db).await? else {
|
let Some(model) = MasterPass::get(user_id, db).await? else {
|
||||||
error!(
|
handle_error(anyhow::anyhow!(
|
||||||
"{:?}",
|
"User was put into the GetMasterPass state with no master password set"
|
||||||
anyhow::anyhow!(
|
));
|
||||||
"User was put into the GetMasterPass state with no master password set"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
return Ok(Some(locale.master_password_is_not_set.to_owned()));
|
return Ok(Some(locale.master_password_is_not_set.to_owned()));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::prelude::*;
|
use crate::{errors::HandlerUsed, prelude::*};
|
||||||
use futures::TryFutureExt;
|
use futures::TryFutureExt;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use std::{borrow::Cow, fmt::Write, path::Path};
|
use std::{borrow::Cow, fmt::Write, path::Path};
|
||||||
|
@ -47,7 +47,7 @@ impl MessageIds {
|
|||||||
|
|
||||||
let result = join!(self.delete(bot), send_request.send());
|
let result = join!(self.delete(bot), send_request.send());
|
||||||
if let Err(err) = result.0 {
|
if let Err(err) = result.0 {
|
||||||
log::error!("{err:?}");
|
handle_error(err);
|
||||||
}
|
}
|
||||||
*self = Self::from(&result.1?);
|
*self = Self::from(&result.1?);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
Reference in New Issue
Block a user