From 59c782f3835689607593af6d8c2bc7d771ed508f Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Fri, 5 Feb 2016 01:43:56 +0100 Subject: [PATCH] qutescheme: handle pdfjs failures more gracefully Now the browser does not crash anymore if an invalid pdfjs resource is requested, instead it will reply with a 404 error. --- qutebrowser/browser/network/qutescheme.py | 33 ++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/qutebrowser/browser/network/qutescheme.py b/qutebrowser/browser/network/qutescheme.py index 876d85e1f..8e8f0f200 100644 --- a/qutebrowser/browser/network/qutescheme.py +++ b/qutebrowser/browser/network/qutescheme.py @@ -58,6 +58,25 @@ def add_handler(name): return namedecorator +class QuteSchemeError(Exception): + + """Exception to signal that a handler should return an ErrorReply. + + Attributes correspond to the arguments in + networkreply.ErrorNetworkReply. + + Attributes: + errorstring: Error string to print. + error: Numerical error value. + """ + + def __init__(self, errorstring, error): + """Constructor.""" + + self.errorstring = errorstring + self.error = error + + class QuteSchemeHandler(schemehandler.SchemeHandler): """Scheme handler for qute: URLs.""" @@ -95,6 +114,9 @@ class QuteSchemeHandler(schemehandler.SchemeHandler): return networkreply.ErrorNetworkReply( request, str(e), QNetworkReply.ContentNotFoundError, self.parent()) + except QuteSchemeError as e: + return networkreply.ErrorNetworkReply( + request, e.errorstring, e.error, self.parent()) mimetype, _encoding = mimetypes.guess_type(request.url().fileName()) if mimetype is None: mimetype = 'text/html' @@ -212,4 +234,13 @@ def qute_settings(win_id, _request): def qute_pdfjs(_win_id, request): """Handler for qute://pdfjs. Return the pdf.js viewer.""" urlpath = request.url().path() - return pdfjs.get_pdfjs_res(urlpath) + try: + return pdfjs.get_pdfjs_res(urlpath) + except pdfjs.PDFJSNotFound as e: + # Logging as the error might get lost otherwise since we're not showing + # the error page if a single asset is missing. This way we don't lose + # information, as the failed pdfjs requests are still in the log. + log.misc.warning( + "pdfjs resource requested but not found: {}".format(e.path)) + raise QuteSchemeError("Can't find pdfjs resource '{}'".format(e.path), + QNetworkReply.ContentNotFoundError)