From e73940cd56f510288ed48d8c8b7698bac088f5bf Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Wed, 4 Mar 2026 14:08:55 +0100 Subject: [PATCH 1/2] Add forbidden handling --- src/spotifyaio/exceptions.py | 3 +++ src/spotifyaio/spotify.py | 5 +++++ tests/test_spotify.py | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/spotifyaio/exceptions.py b/src/spotifyaio/exceptions.py index 6809255f..f7561cb9 100644 --- a/src/spotifyaio/exceptions.py +++ b/src/spotifyaio/exceptions.py @@ -19,3 +19,6 @@ class SpotifyNotFoundError(SpotifyError): class SpotifyRateLimitError(SpotifyError): """Spotify rate limit exception.""" + +class SpotifyForbiddenError(SpotifyError): + """Spotify forbidden (403) exception.""" \ No newline at end of file diff --git a/src/spotifyaio/spotify.py b/src/spotifyaio/spotify.py index a30ae011..475c308d 100644 --- a/src/spotifyaio/spotify.py +++ b/src/spotifyaio/spotify.py @@ -16,6 +16,7 @@ from spotifyaio.exceptions import ( SpotifyConnectionError, SpotifyError, + SpotifyForbiddenError, SpotifyNotFoundError, ) from spotifyaio.models import ( @@ -153,6 +154,10 @@ async def _request( text = await response.text() + if response.status == 403: + # 403 Forbidden + raise SpotifyForbiddenError(text) + if '"status": 404' in text: msg = f"Resource not found: {uri}" raise SpotifyNotFoundError(msg) diff --git a/tests/test_spotify.py b/tests/test_spotify.py index 35c79b7e..a0698595 100644 --- a/tests/test_spotify.py +++ b/tests/test_spotify.py @@ -16,6 +16,7 @@ SpotifyClient, SpotifyConnectionError, SpotifyError, + SpotifyForbiddenError, SpotifyNotFoundError, ) from spotifyaio.models import SearchType @@ -191,6 +192,29 @@ async def test_get_no_playback_state( ) +async def test_user_has_no_access_to_webapi( + authenticated_client: SpotifyClient, + responses: aioresponses, +) -> None: + """Test that SpotifyForbiddenError is raised if user has no access to WebAPI.""" + text="Check settings on developer.spotify.com/dashboard, the user may not be registered." + responses.get( + f"{SPOTIFY_URL}/v1/me/", + status=403, + body=text + ) + with pytest.raises( + SpotifyForbiddenError, + match=text, + ): + await authenticated_client.get_current_user() + responses.assert_called_once_with( + f"{SPOTIFY_URL}/v1/me", + METH_GET, + headers=HEADERS, + json=None, + ) + async def test_transfer_playback( authenticated_client: SpotifyClient, responses: aioresponses, From bf5ccba941bf4f1b5b1eeb550196c363b09e3b91 Mon Sep 17 00:00:00 2001 From: Joostlek Date: Wed, 4 Mar 2026 17:36:05 +0100 Subject: [PATCH 2/2] Fix --- src/spotifyaio/__init__.py | 2 ++ src/spotifyaio/exceptions.py | 3 ++- tests/test_spotify.py | 11 ++++++----- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/spotifyaio/__init__.py b/src/spotifyaio/__init__.py index c074c9df..004777d8 100644 --- a/src/spotifyaio/__init__.py +++ b/src/spotifyaio/__init__.py @@ -4,6 +4,7 @@ SpotifyAuthenticationFailedError, SpotifyConnectionError, SpotifyError, + SpotifyForbiddenError, SpotifyNotFoundError, SpotifyRateLimitError, ) @@ -66,6 +67,7 @@ "SpotifyClient", "SpotifyConnectionError", "SpotifyError", + "SpotifyForbiddenError", "SpotifyNotFoundError", "SpotifyRateLimitError", "Track", diff --git a/src/spotifyaio/exceptions.py b/src/spotifyaio/exceptions.py index f7561cb9..dd868b01 100644 --- a/src/spotifyaio/exceptions.py +++ b/src/spotifyaio/exceptions.py @@ -20,5 +20,6 @@ class SpotifyNotFoundError(SpotifyError): class SpotifyRateLimitError(SpotifyError): """Spotify rate limit exception.""" + class SpotifyForbiddenError(SpotifyError): - """Spotify forbidden (403) exception.""" \ No newline at end of file + """Spotify forbidden (403) exception.""" diff --git a/tests/test_spotify.py b/tests/test_spotify.py index a0698595..965cefad 100644 --- a/tests/test_spotify.py +++ b/tests/test_spotify.py @@ -197,12 +197,11 @@ async def test_user_has_no_access_to_webapi( responses: aioresponses, ) -> None: """Test that SpotifyForbiddenError is raised if user has no access to WebAPI.""" - text="Check settings on developer.spotify.com/dashboard, the user may not be registered." - responses.get( - f"{SPOTIFY_URL}/v1/me/", - status=403, - body=text + text = ( + "Check settings on developer.spotify.com/dashboard, " + "the user may not be registered." ) + responses.get(f"{SPOTIFY_URL}/v1/me", status=403, body=text) with pytest.raises( SpotifyForbiddenError, match=text, @@ -212,9 +211,11 @@ async def test_user_has_no_access_to_webapi( f"{SPOTIFY_URL}/v1/me", METH_GET, headers=HEADERS, + params=None, json=None, ) + async def test_transfer_playback( authenticated_client: SpotifyClient, responses: aioresponses,