Dirk
Dirk

Reputation: 9

Exception thrown even though I'm catching it

I'm trying to enumerate a collection URLs to get corresponding spotify tracks using Spotipy. When a track ID does not exist, the program throws an error, even though I'm catching the exact error mentioned. The error says that during handling of the exception another exception occured, does this mean I've setup my try catch incorrectly?

import spotipy
from spotipy.oauth2 import SpotifyOAuth

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="...",
                                               client_secret="...",
                                               redirect_uri="http://localhost/",
                                               scope="playlist-modify-public",
                                               requests_timeout=5))
try:
    track = sp.track('7IaxPh0ttHTsJ4rVPPiQ9V', 'NL')
except sp.SpotifyException:
    x = 0

This is the error message I get:

Exception has occurred: SpotifyException
http status: 404, code:-1 - https://api.spotify.com/v1/tracks/7IaxPh0ttHTsJ4rVPPiQ9V?market=NL:
 Non existing id: 'spotify:track:7IaxPh0ttHTsJ4rVPPiQ9V', reason: None

During handling of the above exception, another exception occurred:

  File "C:\Users\Dirkv\OneDrive\Documents\Projects\SelfStudy\Python\Music Bot\music_bot.py", line 38, in <module>
    track = sp.track(url, 'NL')

Upvotes: 0

Views: 305

Answers (1)

Elvin Jafarov
Elvin Jafarov

Reputation: 1467

You need to import errors from spotipy not from sp


import spotipy
from spotipy.oauth2 import SpotifyOAuth

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="...",
                                               client_secret="...",
                                               redirect_uri="http://localhost/",
                                               scope="playlist-modify-public",
                                               requests_timeout=5))

try:
    track = sp.track('7IaxPh0ttHTsJ4rVPPiQ9V', 'NL')
except spotipy.SpotifyException :
    x = 0
except spotipy.SpotifyOauthError:
    print("SpotifyOauthError")

NOTE :

DO NOT post your client_id or client_secret for your own safety

Upvotes: 3

Related Questions