blob: 60437469830f0a98f008b8df5f480bfadfd0205f [file] [log] [blame]
Armin Ronacher34f30422007-03-30 21:58:15 +02001import jdebug
2import sys
Armin Ronacher8ff24c42007-03-21 20:33:45 +01003from jinja import Environment, DictLoader
4from jinja.exceptions import TemplateNotFound
Armin Ronacher7977e5c2007-03-12 07:22:17 +01005from wsgiref.simple_server import make_server
Armin Ronacher7977e5c2007-03-12 07:22:17 +01006
Armin Ronacher8ff24c42007-03-21 20:33:45 +01007e = Environment(loader=DictLoader({
8 '/': u'''
9<html>
10 <head>
11 <title>Various Broken Templates</title>
12 <style type="text/css">
13 body {
14 margin: 2em;
15 font-size: 1.5em;
16 font-family: sans-serif
17 }
18 a {
19 color: #d00;
20 }
21 </style>
22 </head>
23 <body>
24 <h1>Various Broken Templates</h1>
25 <p>
26 This small WSGI application serves some Jinja templates that
27 are just broken. It uses the colubrid traceback middleware to
28 render those errors including source code.
29 </p>
30 <ul>
31 <li><a href="syntax_error">syntax error</a></li>
32 <li><a href="runtime_error">runtime error</a></li>
33 <li><a href="nested_syntax_error">nested syntax error</a></li>
34 <li><a href="nested_runtime_error">nested runtime error</a></li>
Armin Ronachera7804ef2007-06-15 18:03:21 +020035 <li><a href="code_runtime_error">runtime error in code</a></li>
Armin Ronacher10dae5b2007-03-31 00:02:32 +020036 <li><a href="syntax_from_string">a syntax error from string</a></li>
37 <li><a href="runtime_from_string">runtime error from a string</a></li>
Armin Ronacher8ff24c42007-03-21 20:33:45 +010038 </ul>
39 </body>
40</html>
41''',
42 '/syntax_error': u'''
43{% for item in foo %}
44 ...
45{% endif %}
46 ''',
47 '/runtime_error': u'''
48{% set foo = 1 / 0 %}
49 ''',
50 '/nested_runtime_error': u'''
51{% include 'runtime_broken' %}
52 ''',
53 '/nested_syntax_error': u'''
54{% include 'syntax_broken' %}
55 ''',
56
Armin Ronachera7804ef2007-06-15 18:03:21 +020057 '/code_runtime_error': u'''
58{{ broken() }}
59''',
60
Armin Ronacher8ff24c42007-03-21 20:33:45 +010061 'runtime_broken': '''\
62This is an included template
63{% set a = 1 / 0 %}''',
64 'syntax_broken': '''\
65This is an included template
66{% raw %}just some foo'''
67}))
68
Armin Ronacher10dae5b2007-03-31 00:02:32 +020069FAILING_STRING_TEMPLATE = '{{ 1 / 0 }}'
70BROKEN_STRING_TEMPLATE = '{% if foo %}...{% endfor %}'
71
Armin Ronacher8ff24c42007-03-21 20:33:45 +010072
Armin Ronachera7804ef2007-06-15 18:03:21 +020073def broken():
74 raise RuntimeError("I'm broken")
75
76
Armin Ronacher8ff24c42007-03-21 20:33:45 +010077def test(environ, start_response):
Armin Ronacher10dae5b2007-03-31 00:02:32 +020078 path = environ.get('PATH_INFO' or '/')
Armin Ronacher8ff24c42007-03-21 20:33:45 +010079 try:
Armin Ronacher10dae5b2007-03-31 00:02:32 +020080 tmpl = e.get_template(path)
Armin Ronacher8ff24c42007-03-21 20:33:45 +010081 except TemplateNotFound:
Armin Ronacher10dae5b2007-03-31 00:02:32 +020082 if path == '/syntax_from_string':
83 tmpl = e.from_string(BROKEN_STRING_TEMPLATE)
84 elif path == '/runtime_from_string':
85 tmpl = e.from_string(FAILING_STRING_TEMPLATE)
86 else:
87 start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])
88 return ['NOT FOUND']
Armin Ronacher8ff24c42007-03-21 20:33:45 +010089 start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
Armin Ronachera7804ef2007-06-15 18:03:21 +020090 return [tmpl.render(broken=broken).encode('utf-8')]
91
Armin Ronacher7977e5c2007-03-12 07:22:17 +010092
Armin Ronacher34f30422007-03-30 21:58:15 +020093if __name__ == '__main__':
Armin Ronachera7804ef2007-06-15 18:03:21 +020094 from werkzeug.debug import DebuggedApplication
Armin Ronacher34f30422007-03-30 21:58:15 +020095 app = DebuggedApplication(test)
96 make_server("localhost", 7000, app).serve_forever()