Merge branch 'fiete201-jinja-error'

This commit is contained in:
Florian Bruhin 2016-12-28 23:31:40 +01:00
commit e192e2795d
3 changed files with 32 additions and 8 deletions

View File

@ -172,6 +172,7 @@ Contributors, sorted by the number of commits in descending order:
* Austin Anderson
* Jimmy
* Niklas Haas
* Fritz Reichwald
* Maciej Wołczyk
* Spreadyy
* Alexey "Averrin" Nabrodov
@ -191,7 +192,6 @@ Contributors, sorted by the number of commits in descending order:
* Michael Hoang
* Liam BEGUIN
* Julie Engel
* Fritz Reichwald
* skinnay
* Zach-Button
* Tomasz Kramkowski

View File

@ -23,6 +23,7 @@ import os
import os.path
import traceback
import mimetypes
import html
import jinja2
import jinja2.exceptions
@ -31,6 +32,24 @@ from qutebrowser.utils import utils, urlutils, log
from PyQt5.QtCore import QUrl
html_fallback = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Error while loading template</title>
</head>
<body>
<p><span style="font-size:120%;color:red">
The %FILE% template could not be found!<br>
Please check your qutebrowser installation
</span><br>
%ERROR%
</p>
</body>
</html>
"""
class Loader(jinja2.BaseLoader):
@ -47,8 +66,11 @@ class Loader(jinja2.BaseLoader):
path = os.path.join(self._subdir, template)
try:
source = utils.read_file(path)
except OSError:
raise jinja2.TemplateNotFound(template)
except OSError as e:
source = html_fallback.replace("%ERROR%", html.escape(str(e)))
source = source.replace("%FILE%", html.escape(template))
log.misc.exception("The {} template could not be loaded from {}"
.format(template, path))
# Currently we don't implement auto-reloading, so we always return True
# for up-to-date.
return source, path, lambda: True

View File

@ -24,7 +24,6 @@ import os.path
import pytest
import logging
import jinja2
from PyQt5.QtCore import QUrl
from qutebrowser.utils import utils, jinja
@ -105,11 +104,14 @@ def test_data_url():
assert data == 'data:text/plain;base64,Zm9v' # 'foo'
def test_not_found():
def test_not_found(caplog):
"""Test with a template which does not exist."""
with pytest.raises(jinja2.TemplateNotFound) as excinfo:
jinja.render('does_not_exist.html')
assert str(excinfo.value) == 'does_not_exist.html'
with caplog.at_level(logging.ERROR):
data = jinja.render('does_not_exist.html')
assert "The does_not_exist.html template could not be found!" in data
assert caplog.records[0].msg.startswith("The does_not_exist.html template"
" could not be loaded from")
def test_utf8():