From 5e916fd7ec3bec30b0c1bac277302fe624fbacc4 Mon Sep 17 00:00:00 2001 From: Konstantin Sobolev Date: Mon, 19 Feb 2024 13:07:34 -0800 Subject: [PATCH] fix: Don't panic if token is still valid 1. `None` from `update_token` should only mean "no update needed", should not be used for errors. We now panic when worker channel is not set 2. Callers should correctly handle `None` result --- src/spotify.rs | 9 ++++----- src/spotify_api.rs | 3 +-- src/ui/search_results.rs | 9 ++++----- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/spotify.rs b/src/spotify.rs index 19665a6..de295ce 100644 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -83,11 +83,10 @@ impl Spotify { spotify.set_volume(volume); spotify.api.set_worker_channel(spotify.channel.clone()); - ASYNC_RUNTIME - .get() - .unwrap() - .block_on(spotify.api.update_token().unwrap()) - .ok(); + spotify + .api + .update_token() + .map(move |h| ASYNC_RUNTIME.get().unwrap().block_on(h).ok()); spotify.api.set_user(user); diff --git a/src/spotify_api.rs b/src/spotify_api.rs index 04b60ed..d3b4a56 100644 --- a/src/spotify_api.rs +++ b/src/spotify_api.rs @@ -117,8 +117,7 @@ impl WebApi { } })) } else { - error!("worker channel is not set"); - None + panic!("worker channel is not set"); } } diff --git a/src/ui/search_results.rs b/src/ui/search_results.rs index 4074cc7..0a604b6 100644 --- a/src/ui/search_results.rs +++ b/src/ui/search_results.rs @@ -392,11 +392,10 @@ impl SearchResultsView { // check if API token refresh is necessary before commencing multiple // requests to avoid deadlock, as the parallel requests might // simultaneously try to refresh the token - ASYNC_RUNTIME - .get() - .unwrap() - .block_on(self.spotify.api.update_token().unwrap()) - .ok(); + self.spotify + .api + .update_token() + .map(move |h| ASYNC_RUNTIME.get().unwrap().block_on(h).ok()); // is the query a Spotify URI? if let Ok(uritype) = query.parse() {