Improve error reporting

This commit is contained in:
Kovid Goyal 2019-10-26 15:34:49 +05:30
parent facc4045d4
commit efb2d424a7
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 24 additions and 12 deletions

View File

@ -120,18 +120,28 @@ def prepare_convert(temp_path, key, st):
}
class ConversionFailure(ValueError):
def __init__(self, book_path, worker_output):
self.book_path = book_path
self.worker_output = worker_output
ValueError.__init__(
self, 'Failed to convert book: {} with error:\n{}'.format(book_path, worker_output))
def do_convert(path, temp_path, key, instance):
tdir = os.path.join(temp_path, instance['path'])
with TemporaryFile('log.txt') as logpath, open(logpath, 'w+b') as logf:
p = start_pipe_worker('from calibre.srv.render_book import viewer_main; viewer_main()', stdout=logf, stderr=logf)
p.stdin.write(msgpack_dumps((
path, tdir, {'size': instance['file_size'], 'mtime': instance['file_mtime'], 'hash': key},
)))
p.stdin.close()
if p.wait() != 0:
with lopen(logpath, 'rb') as logf:
raise Exception('Failed to convert book: {} with errors:\n{}'.format(
path, logf.read().decode('utf-8', 'replace')))
with TemporaryFile('log.txt') as logpath:
with open(logpath, 'w+b') as logf:
p = start_pipe_worker('from calibre.srv.render_book import viewer_main; viewer_main()', stdout=logf, stderr=logf)
p.stdin.write(msgpack_dumps((
path, tdir, {'size': instance['file_size'], 'mtime': instance['file_mtime'], 'hash': key},
)))
p.stdin.close()
if p.wait() != 0:
with lopen(logpath, 'rb') as logf:
worker_output = logf.read().decode('utf-8', 'replace')
raise ConversionFailure(path, worker_output)
size = 0
for f in walk(tdir):
size += os.path.getsize(f)

View File

@ -6,6 +6,7 @@ from __future__ import absolute_import, division, print_function, unicode_litera
import json
import os
import re
import sys
from collections import defaultdict, namedtuple
from hashlib import sha256
@ -333,14 +334,15 @@ class EbookViewer(MainWindow):
open_at, self.pending_open_at = self.pending_open_at, None
if not ok:
self.setWindowTitle(self.base_window_title)
tb = data['tb']
tb = data['tb'].strip()
tb = re.split(r'^calibre\.gui2\.viewer\.convert_book\.ConversionFailure:\s*', tb, maxsplit=1, flags=re.M)[-1]
last_line = tuple(tb.strip().splitlines())[-1]
if last_line.startswith('calibre.ebooks.DRMError'):
DRMErrorMessage(self).exec_()
else:
error_dialog(self, _('Loading book failed'), _(
'Failed to open the book at {0}. Click "Show details" for more info.').format(data['pathtoebook']),
det_msg=data['tb'], show=True)
det_msg=tb, show=True)
self.loading_overlay.hide()
self.web_view.show_home_page()
return