From db3fb9a5eea6e7c48c716fa554d497a12a78b677 Mon Sep 17 00:00:00 2001 From: Niko Date: Tue, 8 Oct 2024 18:40:53 +0200 Subject: [PATCH] created SpotifyBaseException and moved exceptions from oauth2.py to exceptions.py (#1161) --- CHANGELOG.md | 1 + spotipy/exceptions.py | 29 ++++++++++++++++++++++++++++- spotipy/oauth2.py | 24 +----------------------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cab0c577..86ebb8b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/spotipy/exceptions.py b/spotipy/exceptions.py index 28b91419..ba2d5f16 100644 --- a/spotipy/exceptions.py +++ b/spotipy/exceptions.py @@ -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 @@ -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) diff --git a/spotipy/oauth2.py b/spotipy/oauth2.py index 4c0cd906..1591e77d 100644 --- a/spotipy/oauth2.py +++ b/spotipy/oauth2.py @@ -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")