[IPOL discuss] Added traceback for Mako exceptions

Miguel Colom colom at cmla.ens-cachan.fr
Wed Jun 11 14:51:37 CEST 2014


Dear all,
since now, when it raised an exception during a Mako template  
rendering, CherryPy showed a traceback which in fact is not really  
informative. Most of the time, it shows just the name of the exception  
(nameError), but not which variable caused the problem or which file.

I've modified base_app.py in order to use the RichTraceback() object  
from Mako. Now, when a unhandled template exception occurs, a  
different error page is shown, telling which file, line and contents  
of the line caused the failure.

For example:
File /home/miguel/ipolLocal/demo/app/81/template/archive_index.html,  
line 5, in render_body
${nbpublic} public archives out of ${nbtotal} online experiments with

In the example we can see that it was line #5 of file  
archive_index.html which caused the problem, involving the ${nbpublic}  
and ${nbtotal} variables.

You can checkout the new code from git (best option), or change your  
tmpl_out(.) function by this one:
     def tmpl_out(self, tmpl_fname, **kwargs):
         """
         templating shortcut, populated with the default app attributes
         """
         # pass the app object
         kwargs['app'] = self
         # production flag
         kwargs['prod'] = (cherrypy.config['server.environment']
                           == 'production')

         tmpl = self.tmpl_lookup.get_template(tmpl_fname)

         # Render the template
         # If an exception occurs, render an error page showing the traceback
         try:
             return tmpl.render(**kwargs)
         except:
             traceback_string = "<h1>IPOL template rendering error</h1>"
             traceback_string += "<h2>Template: %s</h2>" % tmpl_fname
             traceback_string += "<h2>kwargs: %s</h2>" % kwargs
             traceback = RichTraceback()
             for (filename, lineno, function, line) in traceback.traceback:
                 traceback_string += \
                     "File <b>%s</b>, line <b>%d</b>, in <b>%s</b><br>" % \
                     (filename, lineno, function)
                 traceback_string += line + "<br><br>"
             traceback_string += "%s: %s" % \
                 (str(traceback.error.__class__.__name__), \
                 traceback.error) + "<br>"
             return traceback_string

(also add "from mako.exceptions import RichTraceback" before "from .  
import http").

Best,
Miguel




More information about the discuss mailing list