Cheetah template error rendering
Posted 2010-11-05 15:47:48 by .

In my current job I work with a Django web application used to manage configuration files. Users of the system can create data and templates in the web app. The problem I encountered was displaying errors 'nicely'. This proved difficult to achieve. After a lot of digging and code experimentation I found the solution I was looking for. The code for my solution is in cheetaherrorrender.py


What I wanted, was template errors to be rendered showing which line and what problem occurred. For example I have the following template:

    src = """
    0
    1  hello there $XYZ
    2
    3  $notpresent
    4
    5  $XYZ['notpresent']
    6
    7
    8  A KeyError for an unkown key 'fish': $abc['fish']
    9
    10
    11 ${ 0/0 } A more general error.
    12
    """.strip()

Cheetah is given this data dictionary:

    # Data dict:
    d = dict(XYZ=123, abc={})

My cheetaherrorrender.render_showing_errors(src, d) renders the template as follows:

    Rendered Template:
    ------------------
    0
    1  hello there 123
    2
    ERROR on line 3, Cheetah.NameMapper.NotFound(cannot find 'notpresent'): 3  $notpresent
    4
    ERROR on line 5, exceptions.TypeError(unsubscriptable object):  5  $XYZ['notpresent']
    6
    7
    ERROR on line 8, exceptions.KeyError('fish'):   8  A KeyError for an unkown key 'fish': $abc['fish']
    9
    10
    ERROR on line 11, exceptions.ZeroDivisionError(integer division or modulo by zero):     11 ${ 0/0 } A more general error.
    12
 
This very quickly lets you debug and rectify problems. I can't find an alternative approach to this. I'd welcome suggestions on how I can improve the render_showing_errors function.


References:
 
 * http://www.sourceweaver.com/public/scripts/cheetaherrorrender.py
 * http://www.cheetahtemplate.org

 

Comments

No Comments. Be the first to add one.

New Comment

Your email is never published nor shared.
Required fields are marked *
*
*