Improve error handling for quick-/bookmarks.

This commit is contained in:
Florian Bruhin 2015-07-29 12:34:53 +02:00
parent 093b3cba25
commit c016e8a4cf
2 changed files with 51 additions and 16 deletions

View File

@ -38,7 +38,7 @@ import pygments.formatters
from qutebrowser.commands import userscripts, cmdexc, cmdutils, runners from qutebrowser.commands import userscripts, cmdexc, cmdutils, runners
from qutebrowser.config import config, configexc from qutebrowser.config import config, configexc
from qutebrowser.browser import webelem, inspector from qutebrowser.browser import webelem, inspector, urlmarks
from qutebrowser.keyinput import modeman from qutebrowser.keyinput import modeman
from qutebrowser.utils import (message, usertypes, log, qtutils, urlutils, from qutebrowser.utils import (message, usertypes, log, qtutils, urlutils,
objreg, utils) objreg, utils)
@ -1054,15 +1054,20 @@ class CommandDispatcher:
bg: Load the quickmark in a new background tab. bg: Load the quickmark in a new background tab.
window: Load the quickmark in a new window. window: Load the quickmark in a new window.
""" """
url = objreg.get('quickmark-manager').get(name) try:
url = objreg.get('quickmark-manager').get(name)
except urlmarks.Error as e:
raise cmdexc.CommandError(str(e))
self._open(url, tab, bg, window) self._open(url, tab, bg, window)
@cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.register(instance='command-dispatcher', scope='window')
def bookmark_add(self): def bookmark_add(self):
"""Save the current page as a bookmark.""" """Save the current page as a bookmark."""
bookmark_manager = objreg.get('bookmark-manager') bookmark_manager = objreg.get('bookmark-manager')
bookmark_manager.add(self._win_id, self._current_url(), try:
self._current_title()) bookmark_manager.add(self._current_url(), self._current_title())
except urlmarks.Error as e:
raise cmdexc.CommandError(str(e))
@cmdutils.register(instance='command-dispatcher', scope='window', @cmdutils.register(instance='command-dispatcher', scope='window',
maxsplit=0, maxsplit=0,
@ -1076,7 +1081,11 @@ class CommandDispatcher:
bg: Load the bookmark in a new background tab. bg: Load the bookmark in a new background tab.
window: Load the bookmark in a new window. window: Load the bookmark in a new window.
""" """
self._open(urlutils.fuzzy_url(url), tab, bg, window) try:
url = urlutils.fuzzy_url(url)
except urlutils.FuzzyUrlError as e:
raise cmdexc.CommandError(e)
self._open(url, tab, bg, window)
@cmdutils.register(instance='command-dispatcher', hide=True, @cmdutils.register(instance='command-dispatcher', hide=True,
scope='window') scope='window')

View File

@ -37,6 +37,34 @@ from qutebrowser.commands import cmdexc, cmdutils
from qutebrowser.misc import lineparser from qutebrowser.misc import lineparser
class Error(Exception):
"""Base class for all errors in this module."""
pass
class InvalidUrlError(Error):
"""Exception emitted when a URL is invalid."""
pass
class DoesNotExistError(Error):
"""Exception emitted when a given URL does not exist."""
pass
class AlreadyExistsError(Error):
"""Exception emitted when a given URL does already exist."""
pass
class UrlMarkManager(QObject): class UrlMarkManager(QObject):
"""Base class for BookmarkManager and QuickmarkManager. """Base class for BookmarkManager and QuickmarkManager.
@ -192,18 +220,14 @@ class QuickmarkManager(UrlMarkManager):
def get(self, name): def get(self, name):
"""Get the URL of the quickmark named name as a QUrl.""" """Get the URL of the quickmark named name as a QUrl."""
if name not in self.marks: if name not in self.marks:
raise cmdexc.CommandError( raise DoesNotExistError(
"Quickmark '{}' does not exist!".format(name)) "Quickmark '{}' does not exist!".format(name))
urlstr = self.marks[name] urlstr = self.marks[name]
try: try:
url = urlutils.fuzzy_url(urlstr, do_search=False) url = urlutils.fuzzy_url(urlstr, do_search=False)
except urlutils.FuzzyUrlError as e: except urlutils.FuzzyUrlError as e:
if e.url is None or not e.url.errorString(): raise InvalidUrlError(
errstr = '' "Invalid URL for quickmark {}: {}".format(name, str(e)))
else:
errstr = ' ({})'.format(e.url.errorString())
raise cmdexc.CommandError("Invalid URL for quickmark {}: "
"{}{}".format(name, urlstr, errstr))
return url return url
@ -238,23 +262,25 @@ class BookmarkManager(UrlMarkManager):
elif len(parts) == 1: elif len(parts) == 1:
self.marks[parts[0]] = '' self.marks[parts[0]] = ''
def add(self, win_id, url, title): def add(self, url, title):
"""Add a new bookmark. """Add a new bookmark.
Args: Args:
win_id: The window ID to display the errors in.
url: The url to add as bookmark. url: The url to add as bookmark.
title: The title for the new bookmark. title: The title for the new bookmark.
""" """
if not url.isValid():
errstr = urlutils.get_errstring(url)
raise InvalidUrlError(errstr)
urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded) urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded)
if urlstr in self.marks: if urlstr in self.marks:
message.error(win_id, "Bookmark already exists!") raise AlreadyExistsError("Bookmark already exists!")
else: else:
self.marks[urlstr] = title self.marks[urlstr] = title
self.changed.emit() self.changed.emit()
self.added.emit(title, urlstr) self.added.emit(title, urlstr)
message.info(win_id, "Bookmark added")
@cmdutils.register(instance='bookmark-manager', maxsplit=0, @cmdutils.register(instance='bookmark-manager', maxsplit=0,
completion=[usertypes.Completion.bookmark_by_url]) completion=[usertypes.Completion.bookmark_by_url])