Remove panics in the update_token flow (#688)

It caused a crash when the token failed to update
This commit is contained in:
Csaba Varró
2022-01-01 21:43:12 +01:00
committed by GitHub
parent 4d1388e5bd
commit f0a2cd79ff
2 changed files with 28 additions and 26 deletions

View File

@@ -77,7 +77,8 @@ impl WebApi {
.as_ref() .as_ref()
{ {
channel.send(cmd).expect("can't send message to worker"); channel.send(cmd).expect("can't send message to worker");
let token = futures::executor::block_on(token_rx).unwrap(); let token_option = futures::executor::block_on(token_rx).unwrap();
if let Some(token) = token_option {
*self.api.token.lock().expect("can't writelock api token") = Some(Token { *self.api.token.lock().expect("can't writelock api token") = Some(Token {
access_token: token.access_token, access_token: token.access_token,
expires_in: chrono::Duration::seconds(token.expires_in.into()), expires_in: chrono::Duration::seconds(token.expires_in.into()),
@@ -90,6 +91,9 @@ impl WebApi {
.write() .write()
.expect("could not writelock token") = .expect("could not writelock token") =
Utc::now() + ChronoDuration::seconds(token.expires_in.into()); Utc::now() + ChronoDuration::seconds(token.expires_in.into());
} else {
error!("Failed to update token");
}
} else { } else {
error!("worker channel is not set"); error!("worker channel is not set");
} }

View File

@@ -26,7 +26,7 @@ pub(crate) enum WorkerCommand {
Stop, Stop,
Seek(u32), Seek(u32),
SetVolume(u16), SetVolume(u16),
RequestToken(oneshot::Sender<Token>), RequestToken(oneshot::Sender<Option<Token>>),
Preload(Playable), Preload(Playable),
Shutdown, Shutdown,
} }
@@ -74,7 +74,7 @@ impl Drop for Worker {
impl Worker { impl Worker {
fn get_token( fn get_token(
&self, &self,
sender: oneshot::Sender<Token>, sender: oneshot::Sender<Option<Token>>,
) -> Pin<Box<dyn Future<Output = ()> + Send>> { ) -> Pin<Box<dyn Future<Output = ()> + Send>> {
let client_id = config::CLIENT_ID; let client_id = config::CLIENT_ID;
let scopes = "user-read-private,playlist-read-private,playlist-read-collaborative,playlist-modify-public,playlist-modify-private,user-follow-modify,user-follow-read,user-library-read,user-library-modify,user-top-read,user-read-recently-played"; let scopes = "user-read-private,playlist-read-private,playlist-read-collaborative,playlist-modify-public,playlist-modify-private,user-follow-modify,user-follow-read,user-library-read,user-library-modify,user-top-read,user-read-recently-played";
@@ -87,18 +87,16 @@ impl Worker {
.mercury() .mercury()
.get(url) .get(url)
.map(move |response| { .map(move |response| {
let payload = response response.ok().and_then(move |response| {
.as_ref() let payload = response.payload.first()?;
.unwrap()
.payload let data = String::from_utf8(payload.clone()).ok()?;
.first() let token: Token = serde_json::from_str(&data).ok()?;
.expect("Empty payload");
let data = String::from_utf8(payload.clone()).unwrap();
let token: Token = serde_json::from_str(&data).unwrap();
info!("new token received: {:?}", token); info!("new token received: {:?}", token);
token Some(token)
}) })
.map(|token| sender.send(token).unwrap()), })
.map(|result| sender.send(result).unwrap()),
) )
} }