Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SpotifyBaseException and move all exceptions to exceptions.py #1161

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading