Skip to content

Commit

Permalink
feat: handle transmission connection error (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanaden authored Jun 20, 2024
1 parent 31ce354 commit d526f82
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion rm-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Config {
XDG_DIRS.get_or_init(|| xdg::BaseDirectories::with_prefix("rustmission").unwrap())
}

fn get_config_path() -> &'static PathBuf {
pub fn get_config_path() -> &'static PathBuf {
CONFIG_PATH.get_or_init(|| {
Self::get_xdg_dirs()
.place_config_file("config.toml")
Expand Down
39 changes: 24 additions & 15 deletions rm-main/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
ui::{components::Component, MainWindow},
};

use anyhow::Result;
use anyhow::{Error, Result};
use tokio::sync::{
mpsc::{self, UnboundedReceiver, UnboundedSender},
Mutex,
Expand All @@ -30,15 +30,25 @@ impl Ctx {
config: Config,
action_tx: UnboundedSender<Action>,
trans_tx: UnboundedSender<TorrentAction>,
) -> Self {
let session_info = Arc::new(client.lock().await.session_get().await.unwrap().arguments);

Self {
client,
config: Arc::new(config),
action_tx,
trans_tx,
session_info,
) -> Result<Self> {
let response = client.lock().await.session_get().await;
match response {
Ok(res) => {
let session_info = Arc::new(res.arguments);
return Ok(Self {
client,
config: Arc::new(config),
action_tx,
trans_tx,
session_info,
});
}
Err(e) => {
let config_path = Config::get_config_path().to_str().unwrap();
return Err(Error::msg(format!(
"{e}\nIs the connection info in {config_path} correct?"
)));
}
}
}

Expand All @@ -60,23 +70,22 @@ pub struct App {
}

impl App {
pub async fn new(config: Config) -> Self {
pub async fn new(config: Config) -> Result<Self> {
let (action_tx, action_rx) = mpsc::unbounded_channel();

let client = Arc::new(Mutex::new(transmission::utils::client_from_config(&config)));

let (trans_tx, trans_rx) = mpsc::unbounded_channel();
let ctx = Ctx::new(client, config, action_tx, trans_tx).await;
let ctx = Ctx::new(client, config, action_tx, trans_tx).await?;

tokio::spawn(transmission::action_handler(ctx.clone(), trans_rx));

Self {
Ok(Self {
should_quit: false,
main_window: MainWindow::new(ctx.clone()),
action_rx,
ctx,
mode: Mode::Normal,
}
})
}

pub async fn run(&mut self) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion rm-main/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async fn main() -> Result<()> {
}

async fn run_tui(config: Config) -> Result<()> {
let mut app = App::new(config).await;
let mut app = App::new(config).await?;
app.run().await?;
Ok(())
}

0 comments on commit d526f82

Please sign in to comment.