From 595d6d46f500e2ba5d97c6eacb4cd0c2d6e97ee9 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Mon, 9 Feb 2026 18:13:49 -0600 Subject: [PATCH] fix(spotify): add token retry, HTTP status checking, and diagnostics The Spotify API path was failing silently (no visible logs) and falling back to embed which caps at ~100 tracks. Root cause: HTTP error responses (401/429) were parsed as JSON, then failed with the misleading "missing items" error. Changes: - Add error_for_status() to all HTTP calls (proxy, metadata, tracks) so 401/429/5xx errors surface immediately with real status codes - Retry token + fetch once before falling back to embed (handles transient proxy failures and token expiry) - Descriptive error messages include which request failed --- .../src/playlist_import/providers/spotify.rs | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src-tauri/src/playlist_import/providers/spotify.rs b/src-tauri/src/playlist_import/providers/spotify.rs index 7af7f0eb7..a674d51d4 100644 --- a/src-tauri/src/playlist_import/providers/spotify.rs +++ b/src-tauri/src/playlist_import/providers/spotify.rs @@ -38,22 +38,28 @@ pub fn parse_playlist_id(url: &str) -> Option { pub async fn fetch_playlist( playlist_id: &str, ) -> Result { - if let Ok(token) = get_app_token().await { - log::info!("Spotify: obtained API token, fetching via API"); - match fetch_playlist_with_token(playlist_id, &token).await { - Ok(playlist) => { - log::info!("Spotify: API returned {} tracks", playlist.tracks.len()); - return Ok(playlist); + // Try API with token, retry once with fresh token on failure + for attempt in 1..=2 { + match get_app_token().await { + Ok(token) => { + log::info!("Spotify: API attempt {}/2, token obtained", attempt); + match fetch_playlist_with_token(playlist_id, &token).await { + Ok(playlist) => { + log::info!("Spotify: API returned {} tracks", playlist.tracks.len()); + return Ok(playlist); + } + Err(e) => { + log::warn!("Spotify: API attempt {}/2 failed: {}", attempt, e); + } + } } Err(e) => { - log::warn!("Spotify: API fetch failed ({}), falling back to embed", e); + log::warn!("Spotify: token attempt {}/2 failed: {}", attempt, e); } } - } else { - log::warn!("Spotify: token unavailable, falling back to embed scraping"); } - log::info!("Spotify: using embed fallback (may be limited to ~100 tracks)"); + log::warn!("Spotify: API failed after 2 attempts, falling back to embed (limited to ~100 tracks)"); fetch_playlist_from_embed(playlist_id).await } @@ -71,6 +77,8 @@ async fn fetch_playlist_with_token( .send() .await .map_err(|e| PlaylistImportError::Http(e.to_string()))? + .error_for_status() + .map_err(|e| PlaylistImportError::Http(format!("Spotify metadata: {}", e)))? .json() .await .map_err(|e| PlaylistImportError::Parse(e.to_string()))?; @@ -110,6 +118,8 @@ async fn fetch_playlist_with_token( .send() .await .map_err(|e| PlaylistImportError::Http(e.to_string()))? + .error_for_status() + .map_err(|e| PlaylistImportError::Http(format!("Spotify tracks page offset={}: {}", offset, e)))? .json() .await .map_err(|e| PlaylistImportError::Parse(e.to_string()))?; @@ -312,10 +322,12 @@ async fn get_app_token() -> Result { .get(&url) .send() .await - .map_err(|e| PlaylistImportError::Http(e.to_string()))? + .map_err(|e| PlaylistImportError::Http(format!("Spotify proxy: {}", e)))? + .error_for_status() + .map_err(|e| PlaylistImportError::Http(format!("Spotify proxy: {}", e)))? .json() .await - .map_err(|e| PlaylistImportError::Parse(e.to_string()))?; + .map_err(|e| PlaylistImportError::Parse(format!("Spotify proxy response: {}", e)))?; response .get("access_token")