From 8188f9e304b348f1e7b7bc605c6f29a23e3c8c8b Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Sun, 21 Jun 2020 21:49:32 +0200 Subject: [PATCH] fix: only clear credentials when they're invalid fixes #77 --- src/main.rs | 27 +++++++++++++++++++++++---- src/spotify.rs | 16 ++++++++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index b157040..80b908e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -98,12 +98,23 @@ fn setup_logging(filename: &str) -> Result<(), fern::InitError> { Ok(()) } -fn credentials_prompt(reset: bool) -> Credentials { +fn credentials_prompt(reset: bool, error_message: Option) -> Credentials { let path = config::config_path("credentials.toml"); if reset && fs::remove_file(&path).is_err() { error!("could not delete credential file"); } + if let Some(message) = error_message { + let mut siv = cursive::default(); + let dialog = cursive::views::Dialog::around(cursive::views::TextView::new(format!( + "Connection error:\n{}", + message + ))) + .button("Ok", |s| s.quit()); + siv.add_layer(dialog); + siv.run(); + } + let creds = crate::config::load_or_generate_default(&path, authentication::create_credentials, true) .unwrap_or_else(|e| { @@ -184,12 +195,20 @@ fn main() { info!("Using cached credentials"); c } - None => credentials_prompt(false), + None => credentials_prompt(false, None), } }; - while !spotify::Spotify::test_credentials(credentials.clone()) { - credentials = credentials_prompt(true); + while let Err(error) = spotify::Spotify::test_credentials(credentials.clone()) { + let reset = error + .get_ref() + .map_or(false, |err| err.to_string().contains("BadCredentials")); + debug!("credential reset: {:?}", reset); + let error_msg = match error.get_ref() { + Some(inner) => inner.to_string(), + None => error.to_string(), + }; + credentials = credentials_prompt(reset, Some(error_msg)); } let theme = theme::load(&cfg); diff --git a/src/spotify.rs b/src/spotify.rs index bb28d4c..a96b9ef 100644 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -44,12 +44,12 @@ use url::Url; use core::task::Poll; -use std::env; use std::pin::Pin; use std::sync::atomic::{AtomicU16, Ordering}; use std::sync::RwLock; use std::thread; use std::time::{Duration, SystemTime}; +use std::{env, io}; use crate::artist::Artist; use crate::config; @@ -329,15 +329,23 @@ impl Spotify { session_config } - pub fn test_credentials(credentials: Credentials) -> bool { - let th = thread::spawn(move || { + pub fn test_credentials(credentials: Credentials) -> Result { + let jh = thread::spawn(move || { let mut core = Core::new().unwrap(); let config = Self::session_config(); let handle = core.handle(); core.run(Session::connect(config, credentials, None, handle)) }); - th.join().is_ok() + match jh.join() { + Ok(session) => session.or_else(Err), + Err(e) => Err(io::Error::new( + io::ErrorKind::Other, + e.downcast_ref::() + .unwrap_or(&"N/A".to_string()) + .to_string(), + )), + } } fn create_session(core: &mut Core, cfg: &config::Config, credentials: Credentials) -> Session {