From 8a87dfc65fdaf30a22147dbf88bb884137b498ae Mon Sep 17 00:00:00 2001 From: sigma67 Date: Mon, 11 Jan 2021 11:10:09 +0100 Subject: [PATCH] Add more detail to 4xx error messages --- ytmusicapi/ytmusic.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/ytmusicapi/ytmusic.py b/ytmusicapi/ytmusic.py index 3644b0e..7a18749 100644 --- a/ytmusicapi/ytmusic.py +++ b/ytmusicapi/ytmusic.py @@ -93,12 +93,11 @@ class YTMusic(BrowsingMixin, WatchMixin, LibraryMixin, PlaylistsMixin, UploadsMi # verify authentication credentials work if auth: - self.sapisid = sapisid_from_cookie(self.headers['Cookie']) - response = self._send_request('guide', {}) - if 'error' in response: - raise Exception( - "The provided credentials are invalid. Reason given by the server: " - + response['error']['status']) + try: + self.sapisid = sapisid_from_cookie(self.headers['Cookie']) + except KeyError: + raise Exception("Your cookie is missing the required value __Secure-3PAPISID") + self._send_request('guide', {}) def _send_request(self, endpoint: str, body: Dict, additionalParams: str = "") -> Dict: body.update(self.context) @@ -109,10 +108,13 @@ class YTMusic(BrowsingMixin, WatchMixin, LibraryMixin, PlaylistsMixin, UploadsMi json=body, headers=self.headers, proxies=self.proxies) + response_text = json.loads(response.text) if response.status_code >= 400: - raise Exception("Error: Server returned HTTP " + str(response.status_code) + - ": " + response.reason + ". Please check your credentials.") - return json.loads(response.text) + message = "Server returned HTTP " + str( + response.status_code) + ": " + response.reason + ".\n" + error = response_text.get('error', {}).get('message') + raise Exception(message + error) + return response_text def _check_auth(self): if self.auth == "":