From 4bb42598e1e8e5411e36108af32bcc68acfa03aa Mon Sep 17 00:00:00 2001 From: Callum <40975876+Quizz1Cal@users.noreply.github.com> Date: Wed, 7 Oct 2020 19:00:45 +1100 Subject: [PATCH] Fixed UnboundLocalError / AttributeError #571, #581 (#583) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix plamere/spotipy#571, plamere/spotipy#581 * Add integration test for reaching max retries * Update tests/integration/test_non_user_endpoints.py Co-authored-by: Stéphane Bruckert * Update CHANGELOG.md, integration test mock data Co-authored-by: Stéphane Bruckert --- CHANGELOG.md | 4 +++- spotipy/client.py | 14 ++++++++++---- tests/integration/test_non_user_endpoints.py | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 534f00d0..f446aae8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased -// Add your changes here and then delete this line +### Fixed + +- SpotifyException now thrown when a request fails & has no response ( fixes #571, #581 ) ## [2.16.0] - 2020-09-16 diff --git a/spotipy/client.py b/spotipy/client.py index 6d4ff07d..0d401149 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -243,7 +243,8 @@ def _internal_call(self, method, url, payload, params): response.raise_for_status() results = response.json() - except requests.exceptions.HTTPError: + except requests.exceptions.HTTPError as http_error: + response = http_error.response try: msg = response.json()["error"]["message"] except (ValueError, KeyError): @@ -263,13 +264,18 @@ def _internal_call(self, method, url, payload, params): reason=reason, headers=response.headers, ) - except requests.exceptions.RetryError: + except requests.exceptions.RetryError as retry_error: + request = retry_error.request logger.error('Max Retries reached') + try: + reason = retry_error.args[0].reason + except (IndexError, AttributeError): + reason = None raise SpotifyException( 599, -1, - "%s:\n %s" % (response.url, "Max Retries"), - headers=response.headers, + "%s:\n %s" % (request.path_url, "Max Retries"), + reason=reason ) except ValueError: results = None diff --git a/tests/integration/test_non_user_endpoints.py b/tests/integration/test_non_user_endpoints.py index 02b1a789..a6dcc345 100644 --- a/tests/integration/test_non_user_endpoints.py +++ b/tests/integration/test_non_user_endpoints.py @@ -240,6 +240,20 @@ def test_search_timeout(self): self.assertRaises((requests.exceptions.Timeout, requests.exceptions.ConnectionError), lambda: sp.search(q='my*', type='track')) + def test_max_retries_reached(self): + spotify_no_retry = Spotify( + client_credentials_manager=SpotifyClientCredentials(), + retries=0) + i = 0 + while i < 100: + try: + spotify_no_retry.search(q='foo') + except spotipy.exceptions.SpotifyException as e: + self.assertIsInstance(e, spotipy.exceptions.SpotifyException) + return + i += 1 + self.fail() + def test_album_search(self): results = self.spotify.search(q='weezer pinkerton', type='album') self.assertTrue('albums' in results)