From 36bbf7d15e30bf4e13cd4746e4f6f69ff803845a Mon Sep 17 00:00:00 2001 From: Esamanoaz Date: Thu, 23 Jul 2020 09:08:47 -0700 Subject: [PATCH] Give a reason for player errors in SpotifyException (#543) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added reason to SpotifyException * Updated the changelog * Fixing code style Co-authored-by: Stéphane Bruckert --- CHANGELOG.md | 1 + spotipy/client.py | 5 +++++ spotipy/exceptions.py | 7 ++++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f0f531..ae856cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Proper replacements for all deprecated playlist endpoints (See https://developer.spotify.com/community/news/2018/06/12/changes-to-playlist-uris/ and below) - Allow for OAuth 2.0 authorization by instructing the user to open the URL in a browser instead of opening the browser. +- Reason for 403 error in SpotifyException - Support for the PKCE Auth Flow - Support to advertise different language to Spotify diff --git a/spotipy/client.py b/spotipy/client.py index 469f871..0c5e3d8 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -248,6 +248,10 @@ class Spotify(object): msg = response.json()["error"]["message"] except (ValueError, KeyError): msg = "error" + try: + reason = response.json()["error"]["reason"] + except (ValueError, KeyError): + reason = None logger.error('HTTP Error for %s to %s returned %s due to %s', method, url, response.status_code, msg) @@ -256,6 +260,7 @@ class Spotify(object): response.status_code, -1, "%s:\n %s" % (response.url, msg), + reason=reason, headers=response.headers, ) except requests.exceptions.RetryError: diff --git a/spotipy/exceptions.py b/spotipy/exceptions.py index 5c979a5..df503f1 100644 --- a/spotipy/exceptions.py +++ b/spotipy/exceptions.py @@ -1,9 +1,10 @@ class SpotifyException(Exception): - def __init__(self, http_status, code, msg, headers=None): + def __init__(self, http_status, code, msg, reason=None, headers=None): self.http_status = http_status self.code = code self.msg = msg + self.reason = reason # `headers` is used to support `Retry-After` in the event of a # 429 status code. if headers is None: @@ -11,5 +12,5 @@ class SpotifyException(Exception): self.headers = headers def __str__(self): - return 'http status: {0}, code:{1} - {2}'.format( - self.http_status, self.code, self.msg) + return 'http status: {0}, code:{1} - {2}, reason: {3}'.format( + self.http_status, self.code, self.msg, self.reason)