Skip to content

Commit 31a1f64

Browse files
authored
Handle not found items (#424)
1 parent 64bc6c2 commit 31a1f64

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
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+
SpotifyNotFoundError,
78
)
89
from .models import (
910
Album,
@@ -66,6 +67,7 @@
6667
"SpotifyClient",
6768
"SpotifyConnectionError",
6869
"SpotifyError",
70+
"SpotifyNotFoundError",
6971
"Track",
7072
"UserProfile",
7173
]

src/spotifyaio/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ class SpotifyConnectionError(SpotifyError):
1111

1212
class SpotifyAuthenticationFailedError(SpotifyError):
1313
"""Spotify authentication failed exception."""
14+
15+
16+
class SpotifyNotFoundError(SpotifyError):
17+
"""Spotify not found exception."""

src/spotifyaio/spotify.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import orjson
1313
from yarl import URL
1414

15-
from spotifyaio.exceptions import SpotifyConnectionError
15+
from spotifyaio.exceptions import SpotifyConnectionError, SpotifyNotFoundError
1616
from spotifyaio.models import (
1717
Album,
1818
AlbumsResponse,
@@ -126,7 +126,13 @@ async def _request(
126126
if response.status == 204:
127127
return ""
128128

129-
return await response.text()
129+
text = await response.text()
130+
131+
if '"status": 404' in text:
132+
msg = f"Resource not found: {uri}"
133+
raise SpotifyNotFoundError(msg)
134+
135+
return text
130136

131137
async def _get(self, uri: str, params: dict[str, Any] | None = None) -> str:
132138
"""Handle a GET request to Spotify."""
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"error": {
3+
"status": 404,
4+
"message": "Resource not found"
5+
}
6+
}

tests/test_spotify.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
import pytest
1212
from yarl import URL
1313

14-
from spotifyaio import RepeatMode, SpotifyClient, SpotifyConnectionError
14+
from spotifyaio import (
15+
RepeatMode,
16+
SpotifyClient,
17+
SpotifyConnectionError,
18+
SpotifyNotFoundError,
19+
)
1520

1621
from . import load_fixture
1722
from .const import HEADERS, SPOTIFY_URL
@@ -862,6 +867,23 @@ async def test_get_playlist(
862867
)
863868

864869

870+
async def test_get_not_found_playlist(
871+
responses: aioresponses,
872+
authenticated_client: SpotifyClient,
873+
) -> None:
874+
"""Test retrieving not found playlist."""
875+
responses.get(
876+
f"{SPOTIFY_URL}/v1/playlists/37i9dQZF1DXcBWIGoYBM5M?additional_types=track,episode",
877+
status=200,
878+
body=load_fixture("playlist_not_found.json"),
879+
)
880+
with pytest.raises(
881+
SpotifyNotFoundError,
882+
match="Resource not found: v1/playlists/37i9dQZF1DXcBWIGoYBM5M",
883+
):
884+
await authenticated_client.get_playlist("37i9dQZF1DXcBWIGoYBM5M")
885+
886+
865887
@pytest.mark.parametrize(
866888
"fixture",
867889
[

0 commit comments

Comments
 (0)