[GH-ISSUE #845] Playlist::get panics if the playlist contains tracks from Local Files #424

Closed
opened 2026-02-27 19:30:32 +03:00 by kerem · 5 comments
Owner

Originally created by @RainbowCookie32 on GitHub (Sep 14, 2021).
Original GitHub issue: https://github.com/librespot-org/librespot/issues/845

Seems like SpotifyId::from_base62() can't handle tracks from local files, since it returns Err.

This leads to the unwrap() on the parse function called from Playlist::get (here) to cause a panic and crash my application with:

thread 'main' panicked at 'called 'Result::unwrap()' on an 'Err' value: SpotifyIdError', /home/rainbowcookie/.cargo/registry/src/github.com-1ecc6299db9ec823/librespot-metadata-0.2.0/src/lib.rs:296:54

It'd be nice for the error to be handled instead of just panicking. I don't know if handling local files is possible, but even just ignoring tracks whose SpotifyId returns Err would be a better stop-gag solution in my case.

Originally created by @RainbowCookie32 on GitHub (Sep 14, 2021). Original GitHub issue: https://github.com/librespot-org/librespot/issues/845 Seems like SpotifyId::from_base62() can't handle tracks from local files, since it returns Err. This leads to the unwrap() on the [parse function](https://github.com/librespot-org/librespot/blob/e27992a754b840e9c4591ebd32838cadaf51ebe8/metadata/src/lib.rs#L129) called from Playlist::get ([here](https://github.com/librespot-org/librespot/blob/e27992a754b840e9c4591ebd32838cadaf51ebe8/metadata/src/lib.rs#L297)) to cause a panic and crash my application with: ``` thread 'main' panicked at 'called 'Result::unwrap()' on an 'Err' value: SpotifyIdError', /home/rainbowcookie/.cargo/registry/src/github.com-1ecc6299db9ec823/librespot-metadata-0.2.0/src/lib.rs:296:54 ``` It'd be nice for the error to be handled instead of just panicking. I don't know if handling local files is possible, but even just ignoring tracks whose SpotifyId returns Err would be a better stop-gag solution in my case.
kerem 2026-02-27 19:30:32 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@roderickvd commented on GitHub (Sep 15, 2021):

Yeah, seems right. Do you know your way around Rust to submit a PR? I see more than one Rust project listed on your GitHub profile 😉

<!-- gh-comment-id:919823776 --> @roderickvd commented on GitHub (Sep 15, 2021): Yeah, seems right. Do you know your way around Rust to submit a PR? I see more than one Rust project listed on your GitHub profile 😉
Author
Owner

@RainbowCookie32 commented on GitHub (Sep 15, 2021):

I can give it a spin. Would replacing

SpotifyId::from_base62(uri_parts[2]).unwrap()

with

if let Ok(id) = SpotifyId::from_base62(uri_parts[2]) {
  track
}

be good enough in your opinion? Not gonna lie, I've barely used map() (or most functions with iterators) in my time with Rust.

Edit: It's not

<!-- gh-comment-id:920125258 --> @RainbowCookie32 commented on GitHub (Sep 15, 2021): I can give it a spin. Would replacing ``` SpotifyId::from_base62(uri_parts[2]).unwrap() ``` with ``` if let Ok(id) = SpotifyId::from_base62(uri_parts[2]) { track } ``` be good enough in your opinion? Not gonna lie, I've barely used map() (or most functions with iterators) in my time with Rust. Edit: _It's not_
Author
Owner

@RainbowCookie32 commented on GitHub (Sep 15, 2021):

Cooked this up. If it's fine, I can open a PR for it

<!-- gh-comment-id:920372387 --> @RainbowCookie32 commented on GitHub (Sep 15, 2021): Cooked [this](https://github.com/RainbowCookie32/librespot/commit/b75e8787e97deeba8fe389da407b56387bbadfd6) up. If it's fine, I can open a PR for it
Author
Owner

@ashthespy commented on GitHub (Sep 16, 2021):

Try looking at filter_map :-)
Something like this should be sufficient.. (please do test though!)

diff --git a/metadata/src/lib.rs b/metadata/src/lib.rs
index cf663ce6..2ed9273e 100644
--- a/metadata/src/lib.rs
+++ b/metadata/src/lib.rs
@@ -291,10 +291,10 @@ impl Metadata for Playlist {
             .get_contents()
             .get_items()
             .iter()
-            .map(|item| {
+            .filter_map(|item| {
                 let uri_split = item.get_uri().split(':');
                 let uri_parts: Vec<&str> = uri_split.collect();
-                SpotifyId::from_base62(uri_parts[2]).unwrap()
+                SpotifyId::from_base62(uri_parts[2]).ok()
             })
             .collect::<Vec<_>>();
<!-- gh-comment-id:920774614 --> @ashthespy commented on GitHub (Sep 16, 2021): Try looking at [`filter_map`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.filter_map) :-) Something like this should be sufficient.. (please do test though!) ```diff diff --git a/metadata/src/lib.rs b/metadata/src/lib.rs index cf663ce6..2ed9273e 100644 --- a/metadata/src/lib.rs +++ b/metadata/src/lib.rs @@ -291,10 +291,10 @@ impl Metadata for Playlist { .get_contents() .get_items() .iter() - .map(|item| { + .filter_map(|item| { let uri_split = item.get_uri().split(':'); let uri_parts: Vec<&str> = uri_split.collect(); - SpotifyId::from_base62(uri_parts[2]).unwrap() + SpotifyId::from_base62(uri_parts[2]).ok() }) .collect::<Vec<_>>(); ```
Author
Owner

@RainbowCookie32 commented on GitHub (Sep 16, 2021):

I knew there had to be a nicer way 😅

Seems to work fine, will PR it. Thanks for the help guys!

<!-- gh-comment-id:921040583 --> @RainbowCookie32 commented on GitHub (Sep 16, 2021): I knew there had to be a nicer way 😅 Seems to work fine, will PR it. Thanks for the help guys!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/librespot#424
No description provided.