From 14df72a7a178932f5d4506c0165b214f7083df0c Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 20 May 2015 13:23:02 +0200 Subject: [PATCH] urlutils: Add get_errstring(). --- qutebrowser/utils/urlutils.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index 98637635a..33cb589fc 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -315,20 +315,15 @@ def invalid_url_error(win_id, url, action): if url.isValid(): raise ValueError("Calling invalid_url_error with valid URL {}".format( url.toDisplayString())) - errstring = "Trying to {} with invalid URL".format(action) - if url.errorString(): - errstring += " - {}".format(url.errorString()) + errstring = get_errstring( + url, "Trying to {} with invalid URL".format(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) + raise cmdexc.CommandError(get_errstring(url)) def filename_from_url(url): @@ -359,6 +354,23 @@ def host_tuple(url): return (url.scheme(), url.host(), url.port()) +def get_errstring(url, base="Invalid URL"): + """Get an error string for an URL. + + Args: + url: The URL as a QUrl. + base: The base error string. + + Return: + A new string with url.errorString() is appended if available. + """ + url_error = url.errorString() + if url_error: + return base + " - {}".format(url_error) + else: + return base + + class FuzzyUrlError(Exception): """Exception raised by fuzzy_url on problems.