From 2c3217685b16c5a4711d968b93ebbd902ba215d4 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Sun, 14 Dec 2014 14:08:45 +0100 Subject: [PATCH] utils: Switch to exceptions.FindError for errors. --- mopidy/exceptions.py | 6 ++++++ mopidy/utils/path.py | 10 +++++----- tests/test_exceptions.py | 10 ++++++++++ tests/utils/test_path.py | 10 +++++----- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/mopidy/exceptions.py b/mopidy/exceptions.py index 85169794..4c4a0f6d 100644 --- a/mopidy/exceptions.py +++ b/mopidy/exceptions.py @@ -24,6 +24,12 @@ class ExtensionError(MopidyException): pass +class FindError(MopidyException): + def __init__(self, message, errno=None): + super(FindError, self).__init__(message, errno) + self.errno = errno + + class FrontendError(MopidyException): pass diff --git a/mopidy/utils/path.py b/mopidy/utils/path.py index 54f480b4..4c546ec3 100644 --- a/mopidy/utils/path.py +++ b/mopidy/utils/path.py @@ -10,7 +10,7 @@ import urlparse import glib -from mopidy import compat +from mopidy import compat, exceptions from mopidy.compat import queue @@ -140,7 +140,7 @@ def _find_worker(relative, follow, done, work, results, errors): st = os.lstat(entry) if (st.st_dev, st.st_ino) in parents: - errors[path] = Exception('Sym/hardlink loop found.') + errors[path] = exceptions.FindError('Sym/hardlink loop found.') continue parents = parents + [(st.st_dev, st.st_ino)] @@ -150,12 +150,12 @@ def _find_worker(relative, follow, done, work, results, errors): elif stat.S_ISREG(st.st_mode): results[path] = st elif stat.S_ISLNK(st.st_mode): - errors[path] = Exception('Not following symlinks.') + errors[path] = exceptions.FindError('Not following symlinks.') else: - errors[path] = Exception('Not a file or directory.') + errors[path] = exceptions.FindError('Not a file or directory.') except OSError as e: - errors[path] = e + errors[path] = exceptions.FindError(e.strerror, e.errno) finally: work.task_done() diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index fc19f60a..3420891e 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -20,6 +20,16 @@ class ExceptionsTest(unittest.TestCase): self.assert_(issubclass( exceptions.ExtensionError, exceptions.MopidyException)) + def test_find_error_is_a_mopidy_exception(self): + self.assert_(issubclass( + exceptions.FindError, exceptions.MopidyException)) + + def test_find_error_can_store_an_errno(self): + exc = exceptions.FindError('msg', errno=1234) + + self.assertEqual(exc.message, 'msg') + self.assertEqual(exc.errno, 1234) + def test_frontend_error_is_a_mopidy_exception(self): self.assert_(issubclass( exceptions.FrontendError, exceptions.MopidyException)) diff --git a/tests/utils/test_path.py b/tests/utils/test_path.py index d82bcbe6..4467e07e 100644 --- a/tests/utils/test_path.py +++ b/tests/utils/test_path.py @@ -9,7 +9,7 @@ import unittest import glib -from mopidy import compat +from mopidy import compat, exceptions from mopidy.utils import path import tests @@ -242,7 +242,7 @@ class FindMTimesTest(unittest.TestCase): missing = os.path.join(self.tmpdir, 'does-not-exist') result, errors = path.find_mtimes(missing) self.assertEqual(result, {}) - self.assertEqual(errors, {missing: tests.IsA(OSError)}) + self.assertEqual(errors, {missing: tests.IsA(exceptions.FindError)}) def test_empty_dir(self): """Empty directories should not show up in results""" @@ -295,7 +295,7 @@ class FindMTimesTest(unittest.TestCase): result, errors = path.find_mtimes(self.tmpdir) self.assertEqual({}, result) - self.assertEqual({directory: tests.IsA(OSError)}, errors) + self.assertEqual({directory: tests.IsA(exceptions.FindError)}, errors) def test_symlinks_are_ignored(self): """By default symlinks should be treated as an error""" @@ -305,7 +305,7 @@ class FindMTimesTest(unittest.TestCase): result, errors = path.find_mtimes(self.tmpdir) self.assertEqual(result, {target: tests.any_int}) - self.assertEqual(errors, {link: tests.IsA(Exception)}) + self.assertEqual(errors, {link: tests.IsA(exceptions.FindError)}) def test_symlink_to_file_as_root_is_followed(self): """Passing a symlink as the root should be followed when follow=True""" @@ -327,7 +327,7 @@ class FindMTimesTest(unittest.TestCase): result, errors = path.find_mtimes(link, follow=True) self.assertEqual({}, result) - self.assertEqual({link: tests.IsA(OSError)}, errors) + self.assertEqual({link: tests.IsA(exceptions.FindError)}, errors) def test_symlink_pointing_at_parent_fails(self): """We should detect a loop via the parent and give up on the branch"""