Merge branch 'fiete201-jinja-error'
This commit is contained in:
commit
e192e2795d
@ -172,6 +172,7 @@ Contributors, sorted by the number of commits in descending order:
|
|||||||
* Austin Anderson
|
* Austin Anderson
|
||||||
* Jimmy
|
* Jimmy
|
||||||
* Niklas Haas
|
* Niklas Haas
|
||||||
|
* Fritz Reichwald
|
||||||
* Maciej Wołczyk
|
* Maciej Wołczyk
|
||||||
* Spreadyy
|
* Spreadyy
|
||||||
* Alexey "Averrin" Nabrodov
|
* Alexey "Averrin" Nabrodov
|
||||||
@ -191,7 +192,6 @@ Contributors, sorted by the number of commits in descending order:
|
|||||||
* Michael Hoang
|
* Michael Hoang
|
||||||
* Liam BEGUIN
|
* Liam BEGUIN
|
||||||
* Julie Engel
|
* Julie Engel
|
||||||
* Fritz Reichwald
|
|
||||||
* skinnay
|
* skinnay
|
||||||
* Zach-Button
|
* Zach-Button
|
||||||
* Tomasz Kramkowski
|
* Tomasz Kramkowski
|
||||||
|
@ -23,6 +23,7 @@ import os
|
|||||||
import os.path
|
import os.path
|
||||||
import traceback
|
import traceback
|
||||||
import mimetypes
|
import mimetypes
|
||||||
|
import html
|
||||||
|
|
||||||
import jinja2
|
import jinja2
|
||||||
import jinja2.exceptions
|
import jinja2.exceptions
|
||||||
@ -31,6 +32,24 @@ from qutebrowser.utils import utils, urlutils, log
|
|||||||
|
|
||||||
from PyQt5.QtCore import QUrl
|
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):
|
class Loader(jinja2.BaseLoader):
|
||||||
|
|
||||||
@ -47,8 +66,11 @@ class Loader(jinja2.BaseLoader):
|
|||||||
path = os.path.join(self._subdir, template)
|
path = os.path.join(self._subdir, template)
|
||||||
try:
|
try:
|
||||||
source = utils.read_file(path)
|
source = utils.read_file(path)
|
||||||
except OSError:
|
except OSError as e:
|
||||||
raise jinja2.TemplateNotFound(template)
|
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
|
# Currently we don't implement auto-reloading, so we always return True
|
||||||
# for up-to-date.
|
# for up-to-date.
|
||||||
return source, path, lambda: True
|
return source, path, lambda: True
|
||||||
|
@ -24,7 +24,6 @@ import os.path
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import logging
|
import logging
|
||||||
import jinja2
|
|
||||||
from PyQt5.QtCore import QUrl
|
from PyQt5.QtCore import QUrl
|
||||||
|
|
||||||
from qutebrowser.utils import utils, jinja
|
from qutebrowser.utils import utils, jinja
|
||||||
@ -105,11 +104,14 @@ def test_data_url():
|
|||||||
assert data == 'data:text/plain;base64,Zm9v' # 'foo'
|
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."""
|
"""Test with a template which does not exist."""
|
||||||
with pytest.raises(jinja2.TemplateNotFound) as excinfo:
|
with caplog.at_level(logging.ERROR):
|
||||||
jinja.render('does_not_exist.html')
|
data = jinja.render('does_not_exist.html')
|
||||||
assert str(excinfo.value) == '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():
|
def test_utf8():
|
||||||
|
Loading…
Reference in New Issue
Block a user