Skip to content

Commit 71b8213

Browse files
edenhausjoostlek
andauthored
Add forbidden handling (#765)
* Add forbidden handling * Fix --------- Co-authored-by: Joostlek <joostlek@outlook.com>
1 parent 9a60794 commit 71b8213

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

src/spotifyaio/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
SpotifyAuthenticationFailedError,
55
SpotifyConnectionError,
66
SpotifyError,
7+
SpotifyForbiddenError,
78
SpotifyNotFoundError,
89
SpotifyRateLimitError,
910
)
@@ -66,6 +67,7 @@
6667
"SpotifyClient",
6768
"SpotifyConnectionError",
6869
"SpotifyError",
70+
"SpotifyForbiddenError",
6971
"SpotifyNotFoundError",
7072
"SpotifyRateLimitError",
7173
"Track",

src/spotifyaio/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ class SpotifyNotFoundError(SpotifyError):
1919

2020
class SpotifyRateLimitError(SpotifyError):
2121
"""Spotify rate limit exception."""
22+
23+
24+
class SpotifyForbiddenError(SpotifyError):
25+
"""Spotify forbidden (403) exception."""

src/spotifyaio/spotify.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from spotifyaio.exceptions import (
1717
SpotifyConnectionError,
1818
SpotifyError,
19+
SpotifyForbiddenError,
1920
SpotifyNotFoundError,
2021
)
2122
from spotifyaio.models import (
@@ -153,6 +154,10 @@ async def _request(
153154

154155
text = await response.text()
155156

157+
if response.status == 403:
158+
# 403 Forbidden
159+
raise SpotifyForbiddenError(text)
160+
156161
if '"status": 404' in text:
157162
msg = f"Resource not found: {uri}"
158163
raise SpotifyNotFoundError(msg)

tests/test_spotify.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
SpotifyClient,
1717
SpotifyConnectionError,
1818
SpotifyError,
19+
SpotifyForbiddenError,
1920
SpotifyNotFoundError,
2021
)
2122
from spotifyaio.models import SearchType
@@ -191,6 +192,30 @@ async def test_get_no_playback_state(
191192
)
192193

193194

195+
async def test_user_has_no_access_to_webapi(
196+
authenticated_client: SpotifyClient,
197+
responses: aioresponses,
198+
) -> None:
199+
"""Test that SpotifyForbiddenError is raised if user has no access to WebAPI."""
200+
text = (
201+
"Check settings on developer.spotify.com/dashboard, "
202+
"the user may not be registered."
203+
)
204+
responses.get(f"{SPOTIFY_URL}/v1/me", status=403, body=text)
205+
with pytest.raises(
206+
SpotifyForbiddenError,
207+
match=text,
208+
):
209+
await authenticated_client.get_current_user()
210+
responses.assert_called_once_with(
211+
f"{SPOTIFY_URL}/v1/me",
212+
METH_GET,
213+
headers=HEADERS,
214+
params=None,
215+
json=None,
216+
)
217+
218+
194219
async def test_transfer_playback(
195220
authenticated_client: SpotifyClient,
196221
responses: aioresponses,

0 commit comments

Comments
 (0)