Improve error handling for quick-/bookmarks.
This commit is contained in:
parent
093b3cba25
commit
c016e8a4cf
@ -38,7 +38,7 @@ import pygments.formatters
|
||||
|
||||
from qutebrowser.commands import userscripts, cmdexc, cmdutils, runners
|
||||
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.utils import (message, usertypes, log, qtutils, urlutils,
|
||||
objreg, utils)
|
||||
@ -1054,15 +1054,20 @@ class CommandDispatcher:
|
||||
bg: Load the quickmark in a new background tab.
|
||||
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)
|
||||
|
||||
@cmdutils.register(instance='command-dispatcher', scope='window')
|
||||
def bookmark_add(self):
|
||||
"""Save the current page as a bookmark."""
|
||||
bookmark_manager = objreg.get('bookmark-manager')
|
||||
bookmark_manager.add(self._win_id, self._current_url(),
|
||||
self._current_title())
|
||||
try:
|
||||
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',
|
||||
maxsplit=0,
|
||||
@ -1076,7 +1081,11 @@ class CommandDispatcher:
|
||||
bg: Load the bookmark in a new background tab.
|
||||
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,
|
||||
scope='window')
|
||||
|
@ -37,6 +37,34 @@ from qutebrowser.commands import cmdexc, cmdutils
|
||||
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):
|
||||
|
||||
"""Base class for BookmarkManager and QuickmarkManager.
|
||||
@ -192,18 +220,14 @@ class QuickmarkManager(UrlMarkManager):
|
||||
def get(self, name):
|
||||
"""Get the URL of the quickmark named name as a QUrl."""
|
||||
if name not in self.marks:
|
||||
raise cmdexc.CommandError(
|
||||
raise DoesNotExistError(
|
||||
"Quickmark '{}' does not exist!".format(name))
|
||||
urlstr = self.marks[name]
|
||||
try:
|
||||
url = urlutils.fuzzy_url(urlstr, do_search=False)
|
||||
except urlutils.FuzzyUrlError as e:
|
||||
if e.url is None or not e.url.errorString():
|
||||
errstr = ''
|
||||
else:
|
||||
errstr = ' ({})'.format(e.url.errorString())
|
||||
raise cmdexc.CommandError("Invalid URL for quickmark {}: "
|
||||
"{}{}".format(name, urlstr, errstr))
|
||||
raise InvalidUrlError(
|
||||
"Invalid URL for quickmark {}: {}".format(name, str(e)))
|
||||
return url
|
||||
|
||||
|
||||
@ -238,23 +262,25 @@ class BookmarkManager(UrlMarkManager):
|
||||
elif len(parts) == 1:
|
||||
self.marks[parts[0]] = ''
|
||||
|
||||
def add(self, win_id, url, title):
|
||||
def add(self, url, title):
|
||||
"""Add a new bookmark.
|
||||
|
||||
Args:
|
||||
win_id: The window ID to display the errors in.
|
||||
url: The url to add as 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)
|
||||
|
||||
if urlstr in self.marks:
|
||||
message.error(win_id, "Bookmark already exists!")
|
||||
raise AlreadyExistsError("Bookmark already exists!")
|
||||
else:
|
||||
self.marks[urlstr] = title
|
||||
self.changed.emit()
|
||||
self.added.emit(title, urlstr)
|
||||
message.info(win_id, "Bookmark added")
|
||||
|
||||
@cmdutils.register(instance='bookmark-manager', maxsplit=0,
|
||||
completion=[usertypes.Completion.bookmark_by_url])
|
||||
|
Loading…
Reference in New Issue
Block a user