diff --git a/src/spotifyaio/__init__.py b/src/spotifyaio/__init__.py index c074c9d..004777d 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 6809255..dd868b0 100644 --- a/src/spotifyaio/exceptions.py +++ b/src/spotifyaio/exceptions.py @@ -19,3 +19,7 @@ class SpotifyNotFoundError(SpotifyError): class SpotifyRateLimitError(SpotifyError): """Spotify rate limit exception.""" + + +class SpotifyForbiddenError(SpotifyError): + """Spotify forbidden (403) exception.""" diff --git a/src/spotifyaio/spotify.py b/src/spotifyaio/spotify.py index a30ae01..475c308 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 35c79b7..965cefa 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,30 @@ 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, + params=None, + json=None, + ) + + async def test_transfer_playback( authenticated_client: SpotifyClient, responses: aioresponses,