Loading ...
Sorry, an error occurred while loading the content.

[Cheetahtemplate-discuss] Cheetah error rendering / better template debugging.

Expand Messages
  • Oisin Mulvihill
    Hi There, I wrote a quick article for my blog on this (http://www.sourceweaver.com/posts/better-cheetah-error-renderer). It then occurred to me I should post
    Message 1 of 1 , Nov 5, 2010
    • 0 Attachment
      Hi There,

      I wrote a quick article for my blog on this
      (http://www.sourceweaver.com/posts/better-cheetah-error-renderer). It
      then occurred to me I should post to the cheetah list. I originally
      searched the archives in vain when hunting for a solution. I hope this
      helps the next person looking to do something similar.

      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'. What I wanted was template errors to be rendered showing
      which line and what problem occurred. After a lot of digging and code
      experimentation I found the solution I was looking for.

      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:

      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 my solution. I'd welcome suggestions on how I
      can improve the render_showing_errors(). I've attached my python code
      however you can also get it from my site here:
      http://www.sourceweaver.com/public/scripts/cheetaherrorrender.py

      All the best,

      Oisin
    Your message has been successfully submitted and would be delivered to recipients shortly.