[IPOL discuss] Added traceback for Mako exceptions
Pascal Getreuer
getreuer at cmla.ens-cachan.fr
Fri Jun 13 02:56:26 CEST 2014
Wow, thanks Miguel! I sure have been stumped on Mako errors before, this
will really help.
Pascal
On Wed, Jun 11, 2014 at 5:51 AM, Miguel Colom <colom at cmla.ens-cachan.fr>
wrote:
> 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
>
>
> --
> IPOL - Image Processing On Line - http://ipol.im/
>
> contact edit at ipol.im - http://www.ipol.im/meta/contact/
> news+feeds twitter @IPOL_journal - http://www.ipol.im/meta/feeds/
> announces announce at list.ipol.im - http://tools.ipol.im/mm/announce/
> discussions discuss at list.ipol.im - http://tools.ipol.im/mm/discuss/
>
--
Pascal Getreuer
CMLA, ENS Cachan
getreuer at cmla.ens-cachan.fr
www.getreuer.info
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://tools.ipol.im/mailman/archive/discuss/attachments/20140612/fb9a57d4/attachment-0001.html>
More information about the discuss
mailing list