Wrap mediafile read exceptions in library.ReadError

The ui only handles library Exceptions. Fixes #857.
This commit is contained in:
Thomas Scholtes 2014-07-06 13:19:43 +02:00
parent 38bf08a49e
commit b5426f72a1
3 changed files with 21 additions and 3 deletions

View file

@ -22,7 +22,7 @@ import unicodedata
import time
import re
from unidecode import unidecode
from beets.mediafile import MediaFile, MutagenError
from beets.mediafile import MediaFile, MutagenError, UnreadableFileError
from beets import plugins
from beets import util
from beets.util import bytestring_path, syspath, normpath, samefile
@ -349,7 +349,7 @@ class Item(LibModel):
def read(self, read_path=None):
"""Read the metadata from the associated file.
If ``read_path`` is specified, read metadata from that file
If `read_path` is specified, read metadata from that file
instead. Updates all the properties in `_media_fields`
from the media file.
@ -361,7 +361,7 @@ class Item(LibModel):
read_path = normpath(read_path)
try:
mediafile = MediaFile(syspath(read_path))
except (OSError, IOError) as exc:
except (OSError, IOError, UnreadableFileError) as exc:
raise ReadError(read_path, exc)
for key in self._media_fields:

View file

@ -1243,6 +1243,7 @@ class MediaFile(object):
)
try:
self.mgfile = mutagen.File(path)
print(self.mgfile)
except unreadable_exc as exc:
log.debug(u'header parsing failed: {0}'.format(unicode(exc)))
raise UnreadableFileError(path)

View file

@ -26,6 +26,7 @@ import _common
from _common import unittest
from _common import item
import beets.library
import beets.mediafile
from beets import util
from beets import plugins
from beets import config
@ -1055,6 +1056,22 @@ class WriteTest(_common.LibTestCase):
self.assertNotEqual(MediaFile(self.i.path).artist, 'new artist')
class ItemReadTest(unittest.TestCase):
def test_unreadable_raise_read_error(self):
unreadable = os.path.join(_common.RSRC, 'image-2x3.png')
item = beets.library.Item()
with self.assertRaises(beets.library.ReadError) as cm:
item.read(unreadable)
self.assertIsInstance(cm.exception.reason,
beets.mediafile.UnreadableFileError)
def test_nonexistent_raise_read_error(self):
item = beets.library.Item()
with self.assertRaises(beets.library.ReadError):
item.read('/thisfiledoesnotexist')
def suite():
return unittest.TestLoader().loadTestsFromName(__name__)