Skip to content

Commit

Permalink
created SpotifyBaseException and moved exceptions from oauth2.py to e…
Browse files Browse the repository at this point in the history
…xceptions.py (#1161)
  • Loading branch information
dieser-niko authored Oct 8, 2024
1 parent d9da5af commit db3fb9a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Add your changes below.
- Added custom `urllib3.Retry` class for printing a warning when a rate/request limit is reached.
- Added `personalized_playlist.py`, `track_recommendations.py`, and `audio_features_analysis.py` to `/examples`.
- Discord badge in README
- Added `SpotifyBaseException` and moved all exceptions to `exceptions.py`

### Fixed
- Audiobook integration tests
Expand Down
29 changes: 28 additions & 1 deletion spotipy/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class SpotifyException(Exception):
class SpotifyBaseException(Exception):
pass


class SpotifyException(SpotifyBaseException):

def __init__(self, http_status, code, msg, reason=None, headers=None):
self.http_status = http_status
Expand All @@ -14,3 +18,26 @@ def __init__(self, http_status, code, msg, reason=None, headers=None):
def __str__(self):
return 'http status: {}, code:{} - {}, reason: {}'.format(
self.http_status, self.code, self.msg, self.reason)


class SpotifyOauthError(SpotifyBaseException):
""" Error during Auth Code or Implicit Grant flow """

def __init__(self, message, error=None, error_description=None, *args, **kwargs):
self.error = error
self.error_description = error_description
self.__dict__.update(kwargs)
super().__init__(message, *args, **kwargs)


class SpotifyStateError(SpotifyOauthError):
""" The state sent and state received were different """

def __init__(self, local_state=None, remote_state=None, message=None,
error=None, error_description=None, *args, **kwargs):
if not message:
message = ("Expected " + local_state + " but received "
+ remote_state)
super(SpotifyOauthError, self).__init__(message, error,
error_description, *args,
**kwargs)
24 changes: 1 addition & 23 deletions spotipy/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,12 @@
from urllib.parse import parse_qsl, urlparse

from spotipy.cache_handler import CacheFileHandler, CacheHandler
from spotipy.exceptions import SpotifyOauthError, SpotifyStateError
from spotipy.util import CLIENT_CREDS_ENV_VARS, get_host_port, normalize_scope

logger = logging.getLogger(__name__)


class SpotifyOauthError(Exception):
""" Error during Auth Code or Implicit Grant flow """

def __init__(self, message, error=None, error_description=None, *args, **kwargs):
self.error = error
self.error_description = error_description
self.__dict__.update(kwargs)
super().__init__(message, *args, **kwargs)


class SpotifyStateError(SpotifyOauthError):
""" The state sent and state received were different """

def __init__(self, local_state=None, remote_state=None, message=None,
error=None, error_description=None, *args, **kwargs):
if not message:
message = ("Expected " + local_state + " but received "
+ remote_state)
super(SpotifyOauthError, self).__init__(message, error,
error_description, *args,
**kwargs)


def _make_authorization_headers(client_id, client_secret):
auth_header = base64.b64encode(
str(client_id + ":" + client_secret).encode("ascii")
Expand Down

0 comments on commit db3fb9a

Please sign in to comment.