Propagate refresh token error, fixes #259 (#261)

* Propagate refresh token error #259

https://github.com/plamere/spotipy/issues/259

* Lint

* Format

Co-authored-by: Stephane Bruckert <contact@stephanebruckert.com>
This commit is contained in:
Loisaida Sam 2020-04-02 15:03:24 -04:00 committed by GitHub
parent 5b5b30dd0f
commit ccfda079c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 29 deletions

View File

@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- Close session when Spotipy object is unloaded - Close session when Spotipy object is unloaded
- Propagate refresh token error
## [2.10.0] - 2020-03-18 ## [2.10.0] - 2020-03-18

View File

@ -1,3 +1,4 @@
from .client import * # noqa from .client import * # noqa
from .oauth2 import * # noqa from .oauth2 import * # noqa
from .util import * # noqa from .util import * # noqa
from .exceptions import * # noqa

View File

@ -4,8 +4,6 @@
from __future__ import print_function from __future__ import print_function
__all__ = ["Spotify", "SpotifyException"]
import json import json
import sys import sys
import warnings import warnings
@ -14,22 +12,7 @@ import requests
import urllib3 import urllib3
import six import six
from spotipy.exceptions import SpotifyException
class SpotifyException(Exception):
def __init__(self, http_status, code, msg, headers=None):
self.http_status = http_status
self.code = code
self.msg = msg
# `headers` is used to support `Retry-After` in the event of a
# 429 status code.
if headers is None:
headers = {}
self.headers = headers
def __str__(self):
return "http status: {0}, code:{1} - {2}".format(
self.http_status, self.code, self.msg
)
class Spotify(object): class Spotify(object):

15
spotipy/exceptions.py Normal file
View File

@ -0,0 +1,15 @@
class SpotifyException(Exception):
def __init__(self, http_status, code, msg, headers=None):
self.http_status = http_status
self.code = code
self.msg = msg
# `headers` is used to support `Retry-After` in the event of a
# 429 status code.
if headers is None:
headers = {}
self.headers = headers
def __str__(self):
return 'http status: {0}, code:{1} - {2}'.format(
self.http_status, self.code, self.msg)

View File

@ -18,6 +18,7 @@ import warnings
import requests import requests
from spotipy.util import CLIENT_CREDS_ENV_VARS, get_host_port from spotipy.util import CLIENT_CREDS_ENV_VARS, get_host_port
from spotipy.exceptions import SpotifyException
# Workaround to support both python 2 & 3 # Workaround to support both python 2 & 3
import six import six
@ -273,7 +274,6 @@ class SpotifyOAuth(SpotifyAuthBase):
f.close() f.close()
except IOError: except IOError:
self._warn("couldn't write token cache to " + self.cache_path) self._warn("couldn't write token cache to " + self.cache_path)
pass
def _is_scope_subset(self, needle_scope, haystack_scope): def _is_scope_subset(self, needle_scope, haystack_scope):
needle_scope = set(needle_scope.split()) if needle_scope else set() needle_scope = set(needle_scope.split()) if needle_scope else set()
@ -461,15 +461,17 @@ class SpotifyOAuth(SpotifyAuthBase):
proxies=self.proxies, proxies=self.proxies,
timeout=self.requests_timeout, timeout=self.requests_timeout,
) )
if response.status_code != 200: try:
if False: # debugging code response.raise_for_status()
print("headers", headers) except BaseException:
print("request", response.url) message = "Couldn't refresh token: code:%d reason:%s" % (
self._warn( response.status_code,
"couldn't refresh token: code:%d reason:%s" response.reason,
% (response.status_code, response.reason)
) )
return None raise SpotifyException(response.status_code,
-1,
message,
headers)
token_info = response.json() token_info = response.json()
token_info = self._add_custom_values_to_token_info(token_info) token_info = self._add_custom_values_to_token_info(token_info)
if "refresh_token" not in token_info: if "refresh_token" not in token_info: