Repository: aome510/spotify-player (language: rust)

Issue:
Stuck on loading big playlist
**Describe the bug**
I have a large playlist ~450 songs that I am trying to load and I seem to be getting stuck on loading.

**To Reproduce**
1. Try to open the playlsit Pumped UP Music by djmax444

**Expected behaviour**
The playlist to load.

**Log and backtrace**
```
2024-07-07T19:13:50.022653Z  INFO client_request{request=GetCurrentUserQueue}: spotify_player::client: Successfully handled the client request, took: 275ms
2024-07-07T19:13:50.748507Z  INFO spotify_player::client::handlers: Current context ID (None) is different from the expected ID (Some(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))), update the context state
2024-07-07T19:13:50.748588Z  INFO client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client: Get playlist context: spotify:playlist:7HKmE2T8ahhI5F0xTvqrVB
2024-07-07T19:13:51.749405Z  INFO client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client: Get playlist context: spotify:playlist:7HKmE2T8ahhI5F0xTvqrVB
2024-07-07T19:13:52.416160Z ERROR client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client::handlers: Failed to handle client request: data did not match any variant of untagged enum PlayableItem at line 1 column 218091
2024-07-07T19:13:52.750879Z  INFO client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client: Get playlist context: spotify:playlist:7HKmE2T8ahhI5F0xTvqrVB
2024-07-07T19:13:53.361334Z ERROR client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client::handlers: Failed to handle client request: data did not match any variant of untagged enum PlayableItem at line 1 column 218091
2024-07-07T19:13:53.752646Z  INFO client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client: Get playlist context: spotify:playlist:7HKmE2T8ahhI5F0xTvqrVB
2024-07-07T19:13:54.371317Z ERROR client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client::handlers: Failed to handle client request: data did not match any variant of untagged enum PlayableItem at line 1 column 218091
2024-07-07T19:13:55.290299Z ERROR client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client::handlers: Failed to handle client request: data did not match any variant of untagged enum PlayableItem at line 1 column 218091
2024-07-07T19:13:55.754105Z  INFO spotify_player::client::handlers: Current context ID (None) is different from the expected ID (Some(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))), update the context state
2024-07-07T19:13:55.754163Z  INFO client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client: Get playlist context: spotify:playlist:7HKmE2T8ahhI5F0xTvqrVB
2024-07-07T19:13:56.755475Z  INFO client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client: Get playlist context: spotify:playlist:7HKmE2T8ahhI5F0xTvqrVB
2024-07-07T19:13:57.319011Z ERROR client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client::handlers: Failed to handle client request: data did not match any variant of untagged enum PlayableItem at line 1 column 218091
2024-07-07T19:13:57.756730Z  INFO spotify_player::client::handlers: Current context ID (None) is different from the expected ID (Some(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))), update the context state
2024-07-07T19:13:57.756784Z  INFO client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client: Get playlist context: spotify:playlist:7HKmE2T8ahhI5F0xTvqrVB
2024-07-07T19:13:58.161553Z ERROR client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client::handlers: Failed to handle client request: data did not match any variant of untagged enum PlayableItem at line 1 column 218091
2024-07-07T19:13:59.527344Z ERROR client_request{request=GetContext(Playlist(PlaylistId("7HKmE2T8ahhI5F0xTvqrVB")))}: spotify_player::client::handlers: Failed to handle client request: data did not match any variant of untagged enum PlayableItem at line 1 column 218091
```

**Environment**

- OS: Arch Linux
- Application version: 0.18.2
- Application features: Installed via the AUR

**Additional context**
Add any other context about the problem here.



Discussion / hints:
Might be related to https://github.com/ramsayleung/rspotify/issues/477. Big playlist shouldn't be a problem. Does your playlist contain a local file or something similar?
No it does not, it only has songs added through the spotify official client.

Fix patch:
diff --git a/spotify_player/src/client/mod.rs b/spotify_player/src/client/mod.rs
index 96c89e1b..95dba4e5 100644
--- a/spotify_player/src/client/mod.rs
+++ b/spotify_player/src/client/mod.rs
@@ -9,6 +9,7 @@ use std::io::Write;
 use anyhow::Context as _;
 use anyhow::Result;
 use librespot_core::session::Session;
+use reqwest::StatusCode;
 use rspotify::{
     http::Query,
     model::{FullPlaylist, Market, Page, SimplifiedPlaylist},
@@ -1199,23 +1200,34 @@ impl Client {
 
         // get the artist's information, including top tracks, related artists, and albums
 
-        let artist = self.artist(artist_id.as_ref()).await?.into();
+        let artist = self
+            .artist(artist_id.as_ref())
+            .await
+            .context("get artist")?
+            .into();
 
         let top_tracks = self
             .artist_top_tracks(artist_id.as_ref(), Some(Market::FromToken))
-            .await?;
+            .await
+            .context("get artist's top tracks")?;
         let top_tracks = top_tracks
             .into_iter()
             .filter_map(Track::try_from_full_track)
             .collect::<Vec<_>>();
 
-        let related_artists = self.artist_related_artists(artist_id.as_ref()).await?;
+        let related_artists = self
+            .artist_related_artists(artist_id.as_ref())
+            .await
+            .context("get related artists")?;
         let related_artists = related_artists
             .into_iter()
             .map(|a| a.into())
             .collect::<Vec<_>>();
 
-        let albums = self.artist_albums(artist_id.as_ref()).await?;
+        let albums = self
+            .artist_albums(artist_id.as_ref())
+            .await
+            .context("get artist's albums")?;
 
         Ok(Context::Artist {
             artist,
@@ -1237,6 +1249,9 @@ impl Client {
         fn process_spotify_api_response(text: String) -> String {
             // See: https://github.com/ramsayleung/rspotify/issues/459
             text.replace("\"images\":null", "\"images\":[]")
+                // See: https://github.com/aome510/spotify-player/issues/494
+                // an item's name can be null while Spotify requires it to be available
+                .replace("\"name\":null", "\"name\":\"\"")
         }
 
         let access_token = self.access_token().await?;
@@ -1254,9 +1269,14 @@ impl Client {
             .send()
             .await?;
 
+        let status = response.status();
         let text = process_spotify_api_response(response.text().await?);
         tracing::debug!("{text}");
 
+        if status != StatusCode::OK {
+            anyhow::bail!("failed to send a Spotify API request {url}: {text}");
+        }
+
         Ok(serde_json::from_str(&text)?)
     }
 
@@ -1276,6 +1296,9 @@ impl Client {
             let mut next_page = self
                 .http_get::<rspotify_model::Page<T>>(&url, payload)
                 .await?;
+            if next_page.items.is_empty() {
+                break;
+            }
             items.append(&mut next_page.items);
             maybe_next = next_page.next;
         }


Test patch: