From e895b4ebdee618201db3d898d11102a15f67082a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 26 Mar 2021 16:42:13 +0530 Subject: [PATCH] Better errors when browsing spineless books --- src/calibre/srv/render_book.py | 10 ++++++++++ src/pyj/read_book/view.pyj | 3 +++ 2 files changed, 13 insertions(+) diff --git a/src/calibre/srv/render_book.py b/src/calibre/srv/render_book.py index 7929f99c28..5cb5a707eb 100644 --- a/src/calibre/srv/render_book.py +++ b/src/calibre/srv/render_book.py @@ -59,6 +59,10 @@ RENDER_VERSION = 1 BLANK_JPEG = b'\xff\xd8\xff\xdb\x00C\x00\x03\x02\x02\x02\x02\x02\x03\x02\x02\x02\x03\x03\x03\x03\x04\x06\x04\x04\x04\x04\x04\x08\x06\x06\x05\x06\t\x08\n\n\t\x08\t\t\n\x0c\x0f\x0c\n\x0b\x0e\x0b\t\t\r\x11\r\x0e\x0f\x10\x10\x11\x10\n\x0c\x12\x13\x12\x10\x13\x0f\x10\x10\x10\xff\xc9\x00\x0b\x08\x00\x01\x00\x01\x01\x01\x11\x00\xff\xcc\x00\x06\x00\x10\x10\x05\xff\xda\x00\x08\x01\x01\x00\x00?\x00\xd2\xcf \xff\xd9' # noqa +class Spineless(ValueError): + pass + + def XPath(expr): ans = XPath.cache.get(expr) if ans is None: @@ -841,6 +845,12 @@ def render(pathtoebook, output_dir, book_hash=None, serialize_metadata=False, ex book_hash=book_hash, save_bookmark_data=extract_annotations, book_metadata=mi, virtualize_resources=virtualize_resources ) + try: + has_spine = next(container.spine_names) + except StopIteration: + has_spine = False + if not has_spine: + raise Spineless('Book is empty, no content in spine') if serialize_metadata: from calibre.ebooks.metadata.book.serialize import metadata_as_dict d = metadata_as_dict(mi) diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index 16b992aaf3..0789960f2d 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -927,6 +927,9 @@ class View: if ui_operations.update_last_read_time: ui_operations.update_last_read_time(book) pos = {'replace_history':True} + if not book.manifest.spine.length: + ui_operations.show_error(_('Invalid book'), _('This book is empty, with no items in the spine')) + return name = book.manifest.spine[0] cfi = None if initial_position and initial_position.type is 'cfi' and initial_position.data.startswith('epubcfi(/'):