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,
SpotifyNotFoundError,
)
from .models import (
Album,
Expand Down Expand Up @@ -66,6 +67,7 @@
"SpotifyClient",
"SpotifyConnectionError",
"SpotifyError",
"SpotifyNotFoundError",
"Track",
"UserProfile",
]
4 changes: 4 additions & 0 deletions src/spotifyaio/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ class SpotifyConnectionError(SpotifyError):

class SpotifyAuthenticationFailedError(SpotifyError):
"""Spotify authentication failed exception."""


class SpotifyNotFoundError(SpotifyError):
"""Spotify not found exception."""
10 changes: 8 additions & 2 deletions src/spotifyaio/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import orjson
from yarl import URL

from spotifyaio.exceptions import SpotifyConnectionError
from spotifyaio.exceptions import SpotifyConnectionError, SpotifyNotFoundError
from spotifyaio.models import (
Album,
AlbumsResponse,
Expand Down Expand Up @@ -126,7 +126,13 @@ async def _request(
if response.status == 204:
return ""

return await response.text()
text = await response.text()

if '"status": 404' in text:
msg = f"Resource not found: {uri}"
raise SpotifyNotFoundError(msg)

return text

async def _get(self, uri: str, params: dict[str, Any] | None = None) -> str:
"""Handle a GET request to Spotify."""
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/playlist_not_found.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error": {
"status": 404,
"message": "Resource not found"
}
}
24 changes: 23 additions & 1 deletion tests/test_spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
import pytest
from yarl import URL

from spotifyaio import RepeatMode, SpotifyClient, SpotifyConnectionError
from spotifyaio import (
RepeatMode,
SpotifyClient,
SpotifyConnectionError,
SpotifyNotFoundError,
)

from . import load_fixture
from .const import HEADERS, SPOTIFY_URL
Expand Down Expand Up @@ -862,6 +867,23 @@ async def test_get_playlist(
)


async def test_get_not_found_playlist(
responses: aioresponses,
authenticated_client: SpotifyClient,
) -> None:
"""Test retrieving not found playlist."""
responses.get(
f"{SPOTIFY_URL}/v1/playlists/37i9dQZF1DXcBWIGoYBM5M?additional_types=track,episode",
status=200,
body=load_fixture("playlist_not_found.json"),
)
with pytest.raises(
SpotifyNotFoundError,
match="Resource not found: v1/playlists/37i9dQZF1DXcBWIGoYBM5M",
):
await authenticated_client.get_playlist("37i9dQZF1DXcBWIGoYBM5M")


@pytest.mark.parametrize(
"fixture",
[
Expand Down