diff --git a/src/calibre/ebooks/epub/fix/__init__.py b/src/calibre/ebooks/epub/fix/__init__.py index 5bdbd7c7f6..3eae56484e 100644 --- a/src/calibre/ebooks/epub/fix/__init__.py +++ b/src/calibre/ebooks/epub/fix/__init__.py @@ -11,6 +11,14 @@ from calibre.customize import Plugin class InvalidEpub(ValueError): pass +class ParseError(ValueError): + + def __init__(self, name, desc): + self.name = name + self.desc = desc + ValueError.__init__(self, + _('Failed to parse: %s with error: %s')%(name, desc)) + class ePubFixer(Plugin): supported_platforms = ['windows', 'osx', 'linux'] diff --git a/src/calibre/ebooks/epub/fix/container.py b/src/calibre/ebooks/epub/fix/container.py index b9af66d708..539d886312 100644 --- a/src/calibre/ebooks/epub/fix/container.py +++ b/src/calibre/ebooks/epub/fix/container.py @@ -8,8 +8,9 @@ __docformat__ = 'restructuredtext en' import os, posixpath, urllib, sys, re from lxml import etree +from lxml.etree import XMLSyntaxError -from calibre.ebooks.epub.fix import InvalidEpub +from calibre.ebooks.epub.fix import InvalidEpub, ParseError from calibre import guess_type, prepare_string_for_xml from calibre.ebooks.chardet import xml_to_unicode from calibre.constants import iswindows @@ -148,7 +149,10 @@ class Container(object): return self.cache[name] raw = self.get_raw(name) if name in self.mime_map: - raw = self._parse(raw, self.mime_map[name]) + try: + raw = self._parse(raw, self.mime_map[name]) + except XMLSyntaxError, err: + raise ParseError(name, unicode(err)) self.cache[name] = raw return raw diff --git a/src/calibre/ebooks/epub/fix/main.py b/src/calibre/ebooks/epub/fix/main.py index 3f9ca260b3..fbfe80551d 100644 --- a/src/calibre/ebooks/epub/fix/main.py +++ b/src/calibre/ebooks/epub/fix/main.py @@ -14,6 +14,8 @@ from calibre.utils.zipfile import ZipFile from calibre.utils.logging import default_log from calibre.customize.ui import epub_fixers from calibre.ebooks.epub.fix.container import Container +from calibre.ebooks.epub.fix import ParseError + def option_parser(): parser = OptionParser(usage=_( @@ -50,7 +52,11 @@ def main(args=sys.argv): default_log.error(_('You must specify an epub file')) return epub = os.path.abspath(args[1]) - run(epub, opts, default_log) + try: + run(epub, opts, default_log) + except ParseError, err: + default_log.error(unicode(err)) + raise SystemExit(1) if __name__ == '__main__': main()