Better exception handling in history.

- Show an error message when import fails, not a generic crash dialog
- Raise CommandError when debug-dump-history fails
- Check that the path exists for debug-dump-history
This commit is contained in:
Ryan Roden-Corrent 2017-06-07 20:49:11 -04:00
parent 6ce52f39ae
commit feed9c8936

View File

@ -24,7 +24,7 @@ import time
from PyQt5.QtCore import pyqtSlot, QUrl, QTimer from PyQt5.QtCore import pyqtSlot, QUrl, QTimer
from qutebrowser.commands import cmdutils from qutebrowser.commands import cmdutils, cmdexc
from qutebrowser.utils import (utils, objreg, log, qtutils, usertypes, message, from qutebrowser.utils import (utils, objreg, log, qtutils, usertypes, message,
debug, standarddir) debug, standarddir)
from qutebrowser.misc import objects, sql from qutebrowser.misc import objects, sql
@ -245,10 +245,14 @@ class WebHistory(sql.SqlTable):
def action(): def action():
with debug.log_time(log.init, 'Import old history file to sqlite'): with debug.log_time(log.init, 'Import old history file to sqlite'):
self._read(path) try:
message.info('History import complete. Removing {}' self._read(path)
.format(path)) except ValueError as ex:
os.remove(path) message.error('Failed to import history: {}'.format(ex))
else:
message.info('History import complete. Removing {}'
.format(path))
os.remove(path)
# delay to give message time to appear before locking down for import # delay to give message time to appear before locking down for import
message.info('Converting {} to sqlite...'.format(path)) message.info('Converting {} to sqlite...'.format(path))
@ -268,9 +272,9 @@ class WebHistory(sql.SqlTable):
rows.append(row) rows.append(row)
if completion_row is not None: if completion_row is not None:
completion_rows.append(completion_row) completion_rows.append(completion_row)
except ValueError: except ValueError as ex:
raise Exception('Failed to parse line #{} of {}: "{}"' raise ValueError('Failed to parse line #{} of {}: "{}"'
.format(i, path, line)) .format(i, path, ex))
self.insert_batch(rows) self.insert_batch(rows)
self.completion.insert_batch(completion_rows, replace=True) self.completion.insert_batch(completion_rows, replace=True)
@ -283,6 +287,10 @@ class WebHistory(sql.SqlTable):
""" """
dest = os.path.expanduser(dest) dest = os.path.expanduser(dest)
dirname = os.path.dirname(dest)
if not os.path.exists(dirname):
raise cmdexc.CommandError('Path does not exist', dirname)
lines = ('{}{} {} {}' lines = ('{}{} {} {}'
.format(int(x.atime), '-r' * x.redirect, x.url, x.title) .format(int(x.atime), '-r' * x.redirect, x.url, x.title)
for x in self.select(sort_by='atime', sort_order='asc')) for x in self.select(sort_by='atime', sort_order='asc'))
@ -291,9 +299,8 @@ class WebHistory(sql.SqlTable):
try: try:
f.write('\n'.join(lines)) f.write('\n'.join(lines))
except OSError as e: except OSError as e:
message.error('Could not write history: {}'.format(e)) raise cmdexc.CommandError('Could not write history: {}', e)
else: message.info("Dumped history to {}.".format(dest))
message.info("Dumped history to {}.".format(dest))
def init(parent=None): def init(parent=None):