Spotipy: JSONDecodeError instead of SpotifyOauthError

Created on 5 Sep 2020  路  1Comment  路  Source: plamere/spotipy

Describe the bug
When auth fails, the error handling assumes the response is JSON.

Your code
It's not possible to reproduce because it relies un unreliable network.

Expected behavior
A SpotifyOauthError raised.

Output
Not applicable.

Environment:
Not applicable.

Additional context
I've been getting a LOT of JSONDecodeError on my server and when I investigate the tracebacks I can see the mistake.

The mistake is here: https://github.com/plamere/spotipy/blob/d0ca9776478100c0d3a11bd18a93da14adc990fd/spotipy/oauth2.py#L208
It assumes that the content-type of the response is always JSON but it's clearly not always the truth. As a matter of fact, it might be that response.status_code is 503 or 500.

bug

Most helpful comment

I think the line s

        if response.status_code != 200:
            error_payload = response.json()
            raise SpotifyOauthError(

should be changed to:

        if response.status_code != 200 and response.status_code < 500:
            error_payload = response.json()
            raise SpotifyOauthError(
        elif response.status_code != 200:        
            raise SpotifyServerResponseError(f'{response.status_code} on {self.OAUTH_TOKEN_URL}')

Then, I could in my implementation listen for this error and perhaps swallow the error, do a sleep, and then retry later.

>All comments

I think the line s

        if response.status_code != 200:
            error_payload = response.json()
            raise SpotifyOauthError(

should be changed to:

        if response.status_code != 200 and response.status_code < 500:
            error_payload = response.json()
            raise SpotifyOauthError(
        elif response.status_code != 200:        
            raise SpotifyServerResponseError(f'{response.status_code} on {self.OAUTH_TOKEN_URL}')

Then, I could in my implementation listen for this error and perhaps swallow the error, do a sleep, and then retry later.

Was this page helpful?
0 / 5 - 0 ratings