Index: test/alltests.py =================================================================== --- test/alltests.py (revision 264) +++ test/alltests.py (working copy) @@ -13,7 +13,8 @@ 'filters', 'inheritance', 'call', - 'cache' + 'cache', + 'exceptions_' ) alltests = unittest.TestSuite() for name in modules_to_test: Index: test/exceptions_.py =================================================================== --- test/exceptions_.py (revision 0) +++ test/exceptions_.py (revision 0) @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +import sys +import unittest + +from mako import exceptions +from mako.template import Template + +class ExceptionsTest(unittest.TestCase): + def test_html_error_template(self): + """test the html_error_template""" + code = """ +% i = 0 +""" + try: + template = Template(code) + template.render() + except exceptions.CompileException, ce: + html_error = exceptions.html_error_template().render() + assert ("CompileException: Fragment 'i = 0' is not a partial " + "control statement") in html_error + assert '' in html_error + html_error_stripped = html_error.strip() + assert html_error_stripped.startswith('') + assert html_error_stripped.endswith('') + + not_full = exceptions.html_error_template().render(full=False) + assert '' not in not_full + assert '' not in not_full + assert '' in not_full + + no_css = exceptions.html_error_template().render(css=False) + assert '' not in no_css + else: + assert False, ("This function should trigger a CompileException, " + "but didn't") + + def test_utf8_html_error_template(self): + """test the html_error_template with a Template containing utf8 chars""" + code = """# -*- coding: utf-8 -*- +% if 2 == 2: # an innocently-looking comment +${u'привет'} +% endif +""" + try: + template = Template(code) + template.render() + except exceptions.CompileException, ce: + html_error = exceptions.html_error_template().render() + assert ("CompileException: Fragment 'if 2 == 2: # an " + "innocently-looking comment' is not a partial control " + "statement at line: 2 char: 1") in html_error + assert u"3 ${u'привет'}".encode(sys.getdefaultencoding(), + 'htmlentityreplace') in html_error + else: + assert False, ("This function should trigger a CompileException, " + "but didn't") + +if __name__ == '__main__': + unittest.main() Index: doc/build/content/usage.txt =================================================================== --- doc/build/content/usage.txt (revision 264) +++ doc/build/content/usage.txt (working copy) @@ -148,6 +148,12 @@ except: print exceptions.html_error_template().render() +The `html_error_template` template accepts two options: specifying `full=False` causes only a section of an HTML document to be rendered. Specifying `css=False` will disable the default stylesheet from being rendered. + +E.g.: + + print exceptions.html_error_template().render(full=False) + The HTML render function is also available built-in to `Template` using the `format_exceptions` flag. In this case, any exceptions raised within the **render** stage of the template will result in the output being substituted with the output of `html_error_template`. {python} Index: lib/mako/exceptions.py =================================================================== --- lib/mako/exceptions.py (revision 264) +++ lib/mako/exceptions.py (working copy) @@ -166,15 +166,25 @@ def html_error_template(): """provides a template that renders a stack trace in an HTML format, providing an excerpt of code as well as substituting source template filenames, line numbers and code - for that of the originating source template, as applicable.""" + for that of the originating source template, as applicable. + + the template's default encoding_errors value is 'htmlentityreplace'. the template has + two options: + + with the full option disabled, only a section of an HTML document is returned. + with the css option disabled, the default stylesheet won't be included.""" import mako.template return mako.template.Template(r""" <%! from mako.exceptions import RichTraceback %> +<%page args="full=True, css=True"/> +% if full: Mako Runtime Error +% endif +% if css: +% endif +% if full: +% endif

Error !

<% @@ -221,6 +234,8 @@ % endfor +% if full: +% endif """, output_encoding=sys.getdefaultencoding(), encoding_errors='htmlentityreplace')