From 58793d95d71e603767cb2cdde79525671aee2b57 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 10 Jul 2018 01:17:19 +0200 Subject: [PATCH] Further clean up error handling --- .../browser/webengine/webenginequtescheme.py | 32 ++++++++++--------- .../webkit/network/webkitqutescheme.py | 30 +++++++++-------- tests/unit/browser/test_qutescheme.py | 2 +- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/qutebrowser/browser/webengine/webenginequtescheme.py b/qutebrowser/browser/webengine/webenginequtescheme.py index 1a9b0d72c..a4de0011c 100644 --- a/qutebrowser/browser/webengine/webenginequtescheme.py +++ b/qutebrowser/browser/webengine/webenginequtescheme.py @@ -80,21 +80,23 @@ class QuteSchemeHandler(QWebEngineUrlSchemeHandler): log.misc.debug("Got request for {}".format(url.toDisplayString())) try: mimetype, data = qutescheme.data_for_url(url) - except qutescheme.NotFoundError: - log.misc.exception("Error while handling qute://* URL") - job.fail(QWebEngineUrlRequestJob.UrlNotFound) - except qutescheme.UrlInvalidError: - log.misc.exception("Error while handling qute://* URL") - job.fail(QWebEngineUrlRequestJob.UrlInvalid) - except qutescheme.RequestDeniedError: - log.misc.exception("Error while handling qute://* URL") - job.fail(QWebEngineUrlRequestJob.RequestDenied) - except qutescheme.SchemeOSError: - log.misc.exception("OSError while handling qute://* URL") - job.fail(QWebEngineUrlRequestJob.UrlNotFound) - except qutescheme.Error: - log.misc.exception("Error while handling qute://* URL") - job.fail(QWebEngineUrlRequestJob.RequestFailed) + except qutescheme.Error as e: + errors = { + qutescheme.NotFoundError: + QWebEngineUrlRequestJob.UrlNotFound, + qutescheme.UrlInvalidError: + QWebEngineUrlRequestJob.UrlInvalid, + qutescheme.RequestDeniedError: + QWebEngineUrlRequestJob.RequestDenied, + qutescheme.SchemeOSError: + QWebEngineUrlRequestJob.UrlNotFound, + qutescheme.Error: + QWebEngineUrlRequestJob.RequestFailed, + } + exctype = e.__type__ + log.misc.exception("{} while handling qute://* URL".format( + exctype.__name__)) + job.fail(errors[exctype]) except qutescheme.Redirect as e: qtutils.ensure_valid(e.url) job.redirect(e.url) diff --git a/qutebrowser/browser/webkit/network/webkitqutescheme.py b/qutebrowser/browser/webkit/network/webkitqutescheme.py index a72cef9fa..21951b870 100644 --- a/qutebrowser/browser/webkit/network/webkitqutescheme.py +++ b/qutebrowser/browser/webkit/network/webkitqutescheme.py @@ -59,21 +59,23 @@ def handler(request, operation, current_url): try: mimetype, data = qutescheme.data_for_url(url) - except qutescheme.NotFoundError as e: - return networkreply.ErrorNetworkReply( - request, str(e), QNetworkReply.ContentNotFoundError) - except qutescheme.SchemeOSError as e: - return networkreply.ErrorNetworkReply( - request, str(e), QNetworkReply.ContentNotFoundError) - except qutescheme.UrlInvalidError as e: - return networkreply.ErrorNetworkReply( - request, str(e), QNetworkReply.ContentOperationNotPermittedError) - except qutescheme.RequestDeniedError as e: - return networkreply.ErrorNetworkReply( - request, str(e), QNetworkReply.ContentAccessDenied) except qutescheme.Error as e: - return networkreply.ErrorNetworkReply( - request, str(e), QNetworkReply.InternalServerError) + errors = { + qutescheme.NotFoundError: + QNetworkReply.ContentNotFoundError, + qutescheme.UrlInvalidError: + QNetworkReply.ContentOperationNotPermittedError, + qutescheme.RequestDeniedError: + QNetworkReply.ContentAccessDenied, + qutescheme.SchemeOSError: + QNetworkReply.ContentNotFoundError, + qutescheme.Error: + QNetworkReply.InternalServerError, + } + exctype = e.__type__ + log.misc.exception("{} while handling qute://* URL".format( + exctype.__name__)) + return networkreply.ErrorNetworkReply(request, str(e), errors[exctype]) except qutescheme.Redirect as e: qtutils.ensure_valid(e.url) return networkreply.RedirectNetworkReply(e.url) diff --git a/tests/unit/browser/test_qutescheme.py b/tests/unit/browser/test_qutescheme.py index d9f2d8377..46113561b 100644 --- a/tests/unit/browser/test_qutescheme.py +++ b/tests/unit/browser/test_qutescheme.py @@ -67,7 +67,7 @@ class TestJavascriptHandler: def test_qutejavascript_empty_query(self): url = QUrl("qute://javascript") - with pytest.raises(qutescheme.InvalidURLError): + with pytest.raises(qutescheme.UrlInvalidError): qutescheme.qute_javascript(url)