Propagate refresh token error #259

https://github.com/plamere/spotipy/issues/259
(before upstream support)
This commit is contained in:
LoisaidaSam 2018-02-23 13:37:59 -05:00 committed by David Todd
parent 9c9ae86a23
commit f42801ae90
4 changed files with 33 additions and 24 deletions

View File

@ -1,2 +1,3 @@
VERSION='2.0.1' VERSION='2.0.1'
from .client import Spotify, SpotifyException from .client import Spotify
from .exceptions import SpotifyException

View File

@ -9,26 +9,12 @@ import time
import six import six
from exceptions import SpotifyException
""" A simple and thin Python library for the Spotify Web API """ A simple and thin Python library for the Spotify Web API
""" """
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):
""" """
Example usage:: Example usage::

16
spotipy/exceptions.py Normal file
View File

@ -0,0 +1,16 @@
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

@ -11,6 +11,8 @@ import sys
import six import six
import six.moves.urllib.parse as urllibparse import six.moves.urllib.parse as urllibparse
from exceptions import SpotifyException
class SpotifyOauthError(Exception): class SpotifyOauthError(Exception):
pass pass
@ -236,13 +238,17 @@ class SpotifyOAuth(object):
response = requests.post(self.OAUTH_TOKEN_URL, data=payload, response = requests.post(self.OAUTH_TOKEN_URL, data=payload,
headers=headers, proxies=self.proxies) headers=headers, proxies=self.proxies)
if response.status_code != 200: try:
if False: # debugging code response.raise_for_status()
print('headers', headers) except:
print('request', response.url) message = "Couldn't refresh token: code:%d reason:%s" % (
self._warn("couldn't refresh token: code:%d reason:%s" \ response.status_code,
% (response.status_code, response.reason)) 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 not 'refresh_token' in token_info: if not 'refresh_token' in token_info: