Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/spotifyaio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
SpotifyAuthenticationFailedError,
SpotifyConnectionError,
SpotifyError,
SpotifyForbiddenError,
SpotifyNotFoundError,
SpotifyRateLimitError,
)
Expand Down Expand Up @@ -66,6 +67,7 @@
"SpotifyClient",
"SpotifyConnectionError",
"SpotifyError",
"SpotifyForbiddenError",
"SpotifyNotFoundError",
"SpotifyRateLimitError",
"Track",
Expand Down
4 changes: 4 additions & 0 deletions src/spotifyaio/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ class SpotifyNotFoundError(SpotifyError):

class SpotifyRateLimitError(SpotifyError):
"""Spotify rate limit exception."""


class SpotifyForbiddenError(SpotifyError):
"""Spotify forbidden (403) exception."""
5 changes: 5 additions & 0 deletions src/spotifyaio/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from spotifyaio.exceptions import (
SpotifyConnectionError,
SpotifyError,
SpotifyForbiddenError,
SpotifyNotFoundError,
)
from spotifyaio.models import (
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
SpotifyClient,
SpotifyConnectionError,
SpotifyError,
SpotifyForbiddenError,
SpotifyNotFoundError,
)
from spotifyaio.models import SearchType
Expand Down Expand Up @@ -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,
Expand Down