[GH-ISSUE #1023] Can't catch spotipy.SpotifyException #607

Closed
opened 2026-02-28 00:00:14 +03:00 by kerem · 6 comments
Owner

Originally created by @alexklann on GitHub (Aug 20, 2023).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1023

When using an invalid album name for the query, it will throw an error that can't be caught with a try except:

try:
    results = sp.search(q=album, type="album")
except spotipy.SpotifyException:
    print("Album not found!")
File "/Users/user/PycharmProjects/ChartsGrabber/main.py", line 141, in main
    add_to_spotify("NewJeans", "2nd EP 'Get Up'")
  File "/Users/user/PycharmProjects/ChartsGrabber/main.py", line 44, in add_to_spotify
    result_album_tracks = sp.album_tracks(result_album_id)
  File "/Users/user/PycharmProjects/ChartsGrabber/venv/lib/python3.9/site-packages/spotipy/client.py", line 474, in album_tracks
    trid = self._get_id("album", album_id)
  File "/Users/user/PycharmProjects/ChartsGrabber/venv/lib/python3.9/site-packages/spotipy/client.py", line 1990, in _get_id
    raise SpotifyException(400, -1, "Unsupported URL / URI.")
spotipy.exceptions.SpotifyException: http status: 400, code:-1 - Unsupported URL / URI., reason: None

Originally created by @alexklann on GitHub (Aug 20, 2023). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1023 When using an invalid album name for the query, it will throw an error that can't be caught with a try except: ```python try: results = sp.search(q=album, type="album") except spotipy.SpotifyException: print("Album not found!") ``` ``` File "/Users/user/PycharmProjects/ChartsGrabber/main.py", line 141, in main add_to_spotify("NewJeans", "2nd EP 'Get Up'") File "/Users/user/PycharmProjects/ChartsGrabber/main.py", line 44, in add_to_spotify result_album_tracks = sp.album_tracks(result_album_id) File "/Users/user/PycharmProjects/ChartsGrabber/venv/lib/python3.9/site-packages/spotipy/client.py", line 474, in album_tracks trid = self._get_id("album", album_id) File "/Users/user/PycharmProjects/ChartsGrabber/venv/lib/python3.9/site-packages/spotipy/client.py", line 1990, in _get_id raise SpotifyException(400, -1, "Unsupported URL / URI.") spotipy.exceptions.SpotifyException: http status: 400, code:-1 - Unsupported URL / URI., reason: None ```
kerem 2026-02-28 00:00:14 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@bearsyankees commented on GitHub (Aug 21, 2023):

Could you share more of your code? I am having trouble replicating this issue

<!-- gh-comment-id:1685647509 --> @bearsyankees commented on GitHub (Aug 21, 2023): Could you share more of your code? I am having trouble replicating this issue
Author
Owner

@dieser-niko commented on GitHub (Aug 21, 2023):

I think the error message suggests that the problem might be somewhere else in your code..

<!-- gh-comment-id:1685815212 --> @dieser-niko commented on GitHub (Aug 21, 2023): I think the error message suggests that the problem might be somewhere else in your code..
Author
Owner

@alexklann commented on GitHub (Aug 21, 2023):

https://pastebin.com/W2Zb39BW

Here's the source code.
I don't really think that most of it is even a factor in this issue, since I am using a return directly after calling add_to_spotify() for debug reasons...

<!-- gh-comment-id:1685834389 --> @alexklann commented on GitHub (Aug 21, 2023): https://pastebin.com/W2Zb39BW Here's the source code. I don't really think that most of it is even a factor in this issue, since I am using a return directly after calling `add_to_spotify()` for debug reasons...
Author
Owner

@dieser-niko commented on GitHub (Aug 21, 2023):

So it looks like if you use an invalid album name and the artist can't be found in any of the results, the variable result_album_id will still be an empty string resulting in an error at result_album_tracks = sp.album_tracks(result_album_id) (as the error message suggests)

<!-- gh-comment-id:1685857888 --> @dieser-niko commented on GitHub (Aug 21, 2023): So it looks like if you use an invalid album name and the `artist` can't be found in any of the results, the variable `result_album_id` will still be an empty string resulting in an error at `result_album_tracks = sp.album_tracks(result_album_id)` (as the error message suggests)
Author
Owner

@alexklann commented on GitHub (Aug 21, 2023):

It seems like I removed the return that comes after the print statement in the except-clause before I uploaded the code!
It does seem to be the issue that you've found, however when I add the return statement to the except-clause, the code is never called and does not exit out of the add_to_spotify() function.

<!-- gh-comment-id:1685891228 --> @alexklann commented on GitHub (Aug 21, 2023): It seems like I removed the return that comes after the print statement in the except-clause before I uploaded the code! It does seem to be the issue that you've found, however when I add the return statement to the except-clause, the code is never called and does not exit out of the `add_to_spotify()` function.
Author
Owner

@alexklann commented on GitHub (Aug 21, 2023):

So it seems like using try-except only works when I use it inside of main() with the add_to_spotify function. I guess I don't know how python works...

<!-- gh-comment-id:1686111704 --> @alexklann commented on GitHub (Aug 21, 2023): So it seems like using try-except only works when I use it inside of main() with the add_to_spotify function. I guess I don't know how python works...
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/spotipy#607
No description provided.