Skip to content

Commit

Permalink
library: Fail nicely on lookups when offline
Browse files Browse the repository at this point in the history
  • Loading branch information
jodal committed Jan 20, 2015
1 parent 02ecb8c commit 798ddd3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
27 changes: 16 additions & 11 deletions mopidy_spotify/library.py
Expand Up @@ -30,17 +30,22 @@ def lookup(self, uri):
logger.info('Failed to lookup "%s": %s', uri, exc)
return []

if sp_link.type is spotify.LinkType.TRACK:
return list(self._lookup_track(sp_link))
elif sp_link.type is spotify.LinkType.ALBUM:
return list(self._lookup_album(sp_link))
elif sp_link.type is spotify.LinkType.ARTIST:
return list(self._lookup_artist(sp_link))
elif sp_link.type is spotify.LinkType.PLAYLIST:
return list(self._lookup_playlist(sp_link))
else:
logger.info(
'Failed to lookup "%s": Cannot handle %r', uri, sp_link.type)
try:
if sp_link.type is spotify.LinkType.TRACK:
return list(self._lookup_track(sp_link))
elif sp_link.type is spotify.LinkType.ALBUM:
return list(self._lookup_album(sp_link))
elif sp_link.type is spotify.LinkType.ARTIST:
return list(self._lookup_artist(sp_link))
elif sp_link.type is spotify.LinkType.PLAYLIST:
return list(self._lookup_playlist(sp_link))
else:
logger.info(
'Failed to lookup "%s": Cannot handle %r',
uri, sp_link.type)
return []
except RuntimeError as exc:

This comment has been minimized.

Copy link
@adamcik

adamcik Jan 22, 2015

Member

Huh, this is actually a RuntimeError, should it be something more specific to pyspotify?

This comment has been minimized.

Copy link
@jodal

jodal Jan 25, 2015

Author Member

Yes, it's actually a RuntimeError from pyspotify. Todo added to fix this in pyspotify.

logger.info('Failed to lookup "%s": %s', uri, exc)
return []

def _lookup_track(self, sp_link):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_library.py
Expand Up @@ -57,6 +57,19 @@ def test_lookup_of_unhandled_uri(session_mock, provider, caplog):
in caplog.text())


def test_lookup_when_offline(session_mock, sp_track_mock, provider, caplog):
session_mock.get_link.return_value = sp_track_mock.link
sp_track_mock.link.as_track.return_value.load.side_effect = RuntimeError(
'Must be online to load objects')

results = provider.lookup('spotify:track:abc')

assert len(results) == 0
assert (
'Failed to lookup "spotify:track:abc": Must be online to load objects'
in caplog.text())


def test_lookup_of_track_uri(session_mock, sp_track_mock, provider):
session_mock.get_link.return_value = sp_track_mock.link

Expand Down

0 comments on commit 798ddd3

Please sign in to comment.