From 75d1f072c294a0fd82c1bbf6a6fe1dd45c638041 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 18 Nov 2014 19:49:31 +0100 Subject: [PATCH] Add a raise_cmdexc_if_invalid to urlutils. --- qutebrowser/browser/commands.py | 6 +----- qutebrowser/utils/urlutils.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 0da41165d..22dc1e232 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -109,11 +109,7 @@ class CommandDispatcher: background: Whether to open in the background. window: Whether to open in a new window """ - if not url.isValid(): - errstr = "Invalid URL {}" - if url.errorString(): - errstr += " - {}".format(url.errorString()) - raise cmdexc.CommandError(errstr) + urlutils.raise_cmdexc_if_invalid(url) tabbed_browser = self._tabbed_browser() cmdutils.check_exclusive((tab, background, window), 'tbw') if window: diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index bfa7ef189..b5c07c65e 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -29,6 +29,7 @@ from PyQt5.QtNetwork import QHostInfo from qutebrowser.config import config from qutebrowser.utils import log, qtutils, message +from qutebrowser.commands import cmdexc # FIXME: we probably could raise some exceptions on invalid URLs @@ -272,6 +273,16 @@ def invalid_url_error(win_id, url, action): message.error(win_id, errstring) +def raise_cmdexc_if_invalid(url): + """Check if the given QUrl is invalid, and if so, raise a CommandError.""" + if not url.isValid(): + errstr = "Invalid URL {}".format(url.toDisplayString()) + url_error = url.errorString() + if url_error: + errstr += " - {}".format(url_error) + raise cmdexc.CommandError(errstr) + + class FuzzyUrlError(Exception): """Exception raised by fuzzy_url on problems."""