core: Catch exceptions when browsing in backends

Also splits browse into to method to better distinguish the two possible code
paths.
This commit is contained in:
Thomas Adamcik 2015-04-08 21:12:07 +02:00
parent 928b8df08c
commit 511cf4e326
2 changed files with 23 additions and 17 deletions

View File

@ -68,23 +68,30 @@ class LibraryController(object):
.. versionadded:: 0.18
"""
if uri is None:
directories = set()
backends = self.backends.with_library_browse.values()
futures = {b: b.library.root_directory for b in backends}
for backend, future in futures.items():
try:
directories.add(future.get())
except Exception:
logger.exception('%s backend caused an exception.',
backend.actor_ref.actor_class.__name__)
return sorted(directories, key=operator.attrgetter('name'))
return self._roots() if uri is None else self._browse(uri)
def _roots(self):
directories = set()
backends = self.backends.with_library_browse.values()
futures = {b: b.library.root_directory for b in backends}
for backend, future in futures.items():
try:
directories.add(future.get())
except Exception:
logger.exception('%s backend caused an exception.',
backend.actor_ref.actor_class.__name__)
return sorted(directories, key=operator.attrgetter('name'))
def _browse(self, uri):
scheme = urlparse.urlparse(uri).scheme
backend = self.backends.with_library_browse.get(scheme)
if not backend:
return []
return backend.library.browse(uri).get()
try:
if backend:
return backend.library.browse(uri).get() # TODO: sort?
except Exception:
logger.exception('%s backend caused an exception.',
backend.actor_ref.actor_class.__name__)
return []
def get_distinct(self, field, query=None):
"""

View File

@ -444,10 +444,9 @@ class BackendFailuresCoreLibraryTest(unittest.TestCase):
logger.exception.assert_called_with(mock.ANY, 'DummyBackend')
def test_browse_backend_browse_uri_exception_gets_through(self, logger):
# TODO: is this behavior desired?
self.library.browse.return_value.get.side_effect = Exception
with self.assertRaises(Exception):
self.core.library.browse('dummy:directory')
self.assertEqual([], self.core.library.browse('dummy:directory'))
logger.exception.assert_called_with(mock.ANY, 'DummyBackend')
def test_get_distinct_backend_exception_gets_ignored(self, logger):
self.library.get_distinct.return_value.get.side_effect = Exception