From b8a04f530991b97e8d392035a5043328b2294ed1 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 4 Jan 2015 20:13:25 +0100 Subject: [PATCH] Don't raise CommandError in TabbedBrowser.current_url. TabbedBrowser.current_url used to process the qtutils.QtValueError exception and raise a cmdexc.CommandError based on it. While this was useful for some callers, it made handling it in others weird, and it doesn't really belong there - so now the caller handles this. --- qutebrowser/browser/commands.py | 9 ++++++++- qutebrowser/commands/runners.py | 16 +++++++++++----- qutebrowser/mainwindow/statusbar/command.py | 13 ++++++++++--- qutebrowser/mainwindow/tabbedbrowser.py | 11 ++--------- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index dcca0207e..fe2508440 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -92,7 +92,14 @@ class CommandDispatcher: def _current_url(self): """Convenience method to get the current url.""" - return self._tabbed_browser().current_url() + try: + return self._tabbed_browser().current_url() + except qtutils.QtValueError as e: + msg = "Current URL is invalid" + if e.reason: + msg += " ({})".format(e.reason) + msg += "!" + raise cmdexc.CommandError(msg) def _current_widget(self): """Get the currently active widget from a command.""" diff --git a/qutebrowser/commands/runners.py b/qutebrowser/commands/runners.py index 5ce5bcdfc..dbfcb0303 100644 --- a/qutebrowser/commands/runners.py +++ b/qutebrowser/commands/runners.py @@ -26,7 +26,7 @@ from PyQt5.QtWebKitWidgets import QWebPage from qutebrowser.config import config, configexc from qutebrowser.commands import cmdexc, cmdutils -from qutebrowser.utils import message, log, utils, objreg +from qutebrowser.utils import message, log, utils, objreg, qtutils from qutebrowser.misc import split @@ -35,12 +35,18 @@ def replace_variables(win_id, arglist): args = [] tabbed_browser = objreg.get('tabbed-browser', scope='window', window=win_id) - for arg in arglist: - if arg == '{url}': - # Note we have to do this in here as the user gets an error message - # by current_url if no URL is open yet. + if '{url}' in arglist: + try: url = tabbed_browser.current_url().toString(QUrl.FullyEncoded | QUrl.RemovePassword) + except qtutils.QtValueError as e: + msg = "Current URL is invalid" + if e.reason: + msg += " ({})".format(e.reason) + msg += "!" + raise cmdexc.CommandError(msg) + for arg in arglist: + if arg == '{url}': args.append(url) else: args.append(arg) diff --git a/qutebrowser/mainwindow/statusbar/command.py b/qutebrowser/mainwindow/statusbar/command.py index d5209e5ad..59bd66691 100644 --- a/qutebrowser/mainwindow/statusbar/command.py +++ b/qutebrowser/mainwindow/statusbar/command.py @@ -26,7 +26,7 @@ from qutebrowser.keyinput import modeman, modeparsers from qutebrowser.commands import cmdexc, cmdutils from qutebrowser.misc import cmdhistory from qutebrowser.misc import miscwidgets as misc -from qutebrowser.utils import usertypes, log, objreg +from qutebrowser.utils import usertypes, log, objreg, qtutils class Command(misc.MinimalLineEditMixin, misc.CommandLineEdit): @@ -110,8 +110,15 @@ class Command(misc.MinimalLineEditMixin, misc.CommandLineEdit): tabbed_browser = objreg.get('tabbed-browser', scope='window', window=self._win_id) if '{url}' in text: - url = tabbed_browser.current_url().toString( - QUrl.FullyEncoded | QUrl.RemovePassword) + try: + url = tabbed_browser.current_url().toString( + QUrl.FullyEncoded | QUrl.RemovePassword) + except qtutils.QtValueError as e: + msg = "Current URL is invalid" + if e.reason: + msg += " ({})".format(e.reason) + msg += "!" + raise cmdexc.CommandError(msg) # FIXME we currently replace the URL in any place in the arguments, # rather than just replacing it if it is a dedicated argument. We # could split the args, but then trailing spaces would be lost, so diff --git a/qutebrowser/mainwindow/tabbedbrowser.py b/qutebrowser/mainwindow/tabbedbrowser.py index 8c1c5f448..56733a4ba 100644 --- a/qutebrowser/mainwindow/tabbedbrowser.py +++ b/qutebrowser/mainwindow/tabbedbrowser.py @@ -28,7 +28,6 @@ from PyQt5.QtGui import QIcon from PyQt5.QtWebKitWidgets import QWebPage from qutebrowser.config import config -from qutebrowser.commands import cmdexc from qutebrowser.keyinput import modeman from qutebrowser.mainwindow import tabwidget from qutebrowser.browser import signalfilter, commands, webview @@ -187,14 +186,8 @@ class TabbedBrowser(tabwidget.TabWidget): url = QUrl() else: url = widget.cur_url - try: - qtutils.ensure_valid(url) - except qtutils.QtValueError as e: - msg = "Current URL is invalid" - if e.reason: - msg += " ({})".format(e.reason) - msg += "!" - raise cmdexc.CommandError(msg) + # It's possible for url to be invalid, but the caller will handle that. + qtutils.ensure_valid(url) return url def shutdown(self):