created SpotifyBaseException and moved exceptions from oauth2.py to exceptions.py (#1161)

This commit is contained in:
Niko 2024-10-08 18:40:53 +02:00 committed by GitHub
parent d9da5af53c
commit db3fb9a5ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 24 deletions

View File

@ -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 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`. - Added `personalized_playlist.py`, `track_recommendations.py`, and `audio_features_analysis.py` to `/examples`.
- Discord badge in README - Discord badge in README
- Added `SpotifyBaseException` and moved all exceptions to `exceptions.py`
### Fixed ### Fixed
- Audiobook integration tests - Audiobook integration tests

View File

@ -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): def __init__(self, http_status, code, msg, reason=None, headers=None):
self.http_status = http_status self.http_status = http_status
@ -14,3 +18,26 @@ class SpotifyException(Exception):
def __str__(self): def __str__(self):
return 'http status: {}, code:{} - {}, reason: {}'.format( return 'http status: {}, code:{} - {}, reason: {}'.format(
self.http_status, self.code, self.msg, self.reason) 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)

View File

@ -20,34 +20,12 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import parse_qsl, urlparse from urllib.parse import parse_qsl, urlparse
from spotipy.cache_handler import CacheFileHandler, CacheHandler 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 from spotipy.util import CLIENT_CREDS_ENV_VARS, get_host_port, normalize_scope
logger = logging.getLogger(__name__) 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): def _make_authorization_headers(client_id, client_secret):
auth_header = base64.b64encode( auth_header = base64.b64encode(
str(client_id + ":" + client_secret).encode("ascii") str(client_id + ":" + client_secret).encode("ascii")