implement YtDlpError

This commit is contained in:
mykola2312 2024-02-19 13:46:27 +02:00
parent 2e57b92e32
commit e958d4ef0e
2 changed files with 27 additions and 2 deletions

View file

@ -1,6 +1,7 @@
use serde::Deserialize;
use serde_json;
use std::process::Output;
use core::fmt;
use std::str::Utf8Error;
use tokio::process::Command;
#[derive(Deserialize, Debug)]
@ -24,8 +25,22 @@ pub struct YtDlpInfo {
pub formats: Vec<YtDlpFormat>,
}
#[derive(Debug)]
pub enum YtDlpError {
CommandError(std::io::Error),
UtfError(Utf8Error),
ErrorMessage(String)
}
impl fmt::Display for YtDlpError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use YtDlpError as YTE;
match self {
YTE::CommandError(e) => write!(f, "Command::new - {}", e),
YTE::UtfError(_) => write!(f, "Error while decoding UTF8"),
YTE::ErrorMessage(msg) => write!(f, "yt-dlp error - {}", msg)
}
}
}
pub struct YtDlp {}
@ -41,6 +56,12 @@ impl YtDlp {
Err(e) => return Err(YtDlpError::CommandError(e)),
};
if output.stdout.is_empty() && !output.stderr.is_empty() {
return match std::str::from_utf8(&output.stderr) {
Ok(message) => Err(YtDlpError::ErrorMessage(message.to_string())),
Err(utf8_error) => Err(YtDlpError::UtfError(utf8_error))
};
}
Ok(())
}
}

View file

@ -11,6 +11,7 @@ use teloxide::dispatching::UpdateHandler;
use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands};
mod dl;
use dl::yt_dlp::YtDlp;
type State = ();
type MyDialogue = Dialogue<State, InMemStorage<State>>;
@ -35,7 +36,10 @@ where
async fn main() -> anyhow::Result<()> {
dotenv().ok();
bot_main().await
let _ = YtDlp::load_info(env::var("TEST_URL")?.as_str()).await;
Ok(())
//bot_main().await
}
async fn bot_main() -> anyhow::Result<()> {