Skip to content

Commit db3fb9a

Browse files
authored
created SpotifyBaseException and moved exceptions from oauth2.py to exceptions.py (#1161)
1 parent d9da5af commit db3fb9a

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Add your changes below.
1818
- Added custom `urllib3.Retry` class for printing a warning when a rate/request limit is reached.
1919
- Added `personalized_playlist.py`, `track_recommendations.py`, and `audio_features_analysis.py` to `/examples`.
2020
- Discord badge in README
21+
- Added `SpotifyBaseException` and moved all exceptions to `exceptions.py`
2122

2223
### Fixed
2324
- Audiobook integration tests

spotipy/exceptions.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
class SpotifyException(Exception):
1+
class SpotifyBaseException(Exception):
2+
pass
3+
4+
5+
class SpotifyException(SpotifyBaseException):
26

37
def __init__(self, http_status, code, msg, reason=None, headers=None):
48
self.http_status = http_status
@@ -14,3 +18,26 @@ def __init__(self, http_status, code, msg, reason=None, headers=None):
1418
def __str__(self):
1519
return 'http status: {}, code:{} - {}, reason: {}'.format(
1620
self.http_status, self.code, self.msg, self.reason)
21+
22+
23+
class SpotifyOauthError(SpotifyBaseException):
24+
""" Error during Auth Code or Implicit Grant flow """
25+
26+
def __init__(self, message, error=None, error_description=None, *args, **kwargs):
27+
self.error = error
28+
self.error_description = error_description
29+
self.__dict__.update(kwargs)
30+
super().__init__(message, *args, **kwargs)
31+
32+
33+
class SpotifyStateError(SpotifyOauthError):
34+
""" The state sent and state received were different """
35+
36+
def __init__(self, local_state=None, remote_state=None, message=None,
37+
error=None, error_description=None, *args, **kwargs):
38+
if not message:
39+
message = ("Expected " + local_state + " but received "
40+
+ remote_state)
41+
super(SpotifyOauthError, self).__init__(message, error,
42+
error_description, *args,
43+
**kwargs)

spotipy/oauth2.py

+1-23
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,12 @@
2020
from urllib.parse import parse_qsl, urlparse
2121

2222
from spotipy.cache_handler import CacheFileHandler, CacheHandler
23+
from spotipy.exceptions import SpotifyOauthError, SpotifyStateError
2324
from spotipy.util import CLIENT_CREDS_ENV_VARS, get_host_port, normalize_scope
2425

2526
logger = logging.getLogger(__name__)
2627

2728

28-
class SpotifyOauthError(Exception):
29-
""" Error during Auth Code or Implicit Grant flow """
30-
31-
def __init__(self, message, error=None, error_description=None, *args, **kwargs):
32-
self.error = error
33-
self.error_description = error_description
34-
self.__dict__.update(kwargs)
35-
super().__init__(message, *args, **kwargs)
36-
37-
38-
class SpotifyStateError(SpotifyOauthError):
39-
""" The state sent and state received were different """
40-
41-
def __init__(self, local_state=None, remote_state=None, message=None,
42-
error=None, error_description=None, *args, **kwargs):
43-
if not message:
44-
message = ("Expected " + local_state + " but received "
45-
+ remote_state)
46-
super(SpotifyOauthError, self).__init__(message, error,
47-
error_description, *args,
48-
**kwargs)
49-
50-
5129
def _make_authorization_headers(client_id, client_secret):
5230
auth_header = base64.b64encode(
5331
str(client_id + ":" + client_secret).encode("ascii")

0 commit comments

Comments
 (0)