Improved error handling in GetUser state handler

This commit is contained in:
2024-02-10 19:28:16 +03:00
parent a359364606
commit 40515c463a
2 changed files with 70 additions and 65 deletions

View File

@ -1,7 +1,7 @@
use crate::prelude::*;
use futures::TryFutureExt;
use itertools::Itertools;
use std::{fmt::Write, path::Path};
use std::{borrow::Cow, fmt::Write, path::Path};
use teloxide::{
net::Download,
types::{Document, FileMeta},
@ -128,7 +128,38 @@ fn user_from_vec(
}
}
#[inline]
async fn user_from_document(
bot: &Throttle<Bot>,
db: &Pool,
document: Option<&Document>,
user_id: u64,
) -> Result<User, Cow<'static, str>> {
let (data, existing_names) = {
let file = match validate_document(document) {
Ok(document) => &document.file,
Err(text) => return Err(Cow::Borrowed(text)),
};
let data =
download_file(bot, file).map_err(|_| "Error getting existing account names. Try again");
let existing_names = Account::get_names(user_id, db)
.try_collect()
.map_err(|_| "Error downloading the file. Try again");
try_join!(data, existing_names)?
};
match spawn_blocking(|| user_from_vec(data, existing_names)).await {
Ok(Ok(Ok(user))) => Ok(user),
Ok(Ok(Err(error_text))) => Err(Cow::Owned(error_text)),
_ => Err(Cow::Borrowed("Error parsing the json file. Try again")),
}
}
/// Function to handle `GetUser` state. It doesn't actually validate anything
#[inline]
pub async fn get_user(
bot: Throttle<Bot>,
msg: Message,
@ -153,39 +184,15 @@ pub async fn get_user(
return Ok(());
}
let file = match validate_document(msg.document()) {
Ok(document) => &document.file,
Err(text) => {
handler
.previous
.alter_message(&bot, text, None, None)
.await?;
return Ok(());
}
};
let existing_names = Account::get_names(user_id, &db)
.try_collect()
.map_err(Into::into);
let (data, existing_names) = try_join!(download_file(&bot, file), existing_names)?;
let user = match spawn_blocking(move || user_from_vec(data, existing_names)).await? {
Ok(Ok(user)) => user,
Ok(Err(error_text)) => {
let user = match user_from_document(&bot, &db, msg.document(), user_id).await {
Ok(user) => user,
Err(error_text) => {
handler
.previous
.alter_message(&bot, error_text, None, None)
.await?;
return Ok(());
}
Err(_) => {
handler
.previous
.alter_message(&bot, "Error parsing the json file. Try again", None, None)
.await?;
return Ok(());
}
};
let previous = handler.previous;