librelist archives

« back to archive

How to set up mako templates with Flask to yield decipherable exceptions?

How to set up mako templates with Flask to yield decipherable exceptions?

From:
chrickso
Date:
2012-05-30 @ 00:37
Hi everyone,

Please see my stackoverflow post for more details:

http://stackoverflow.com/questions/10807132/how-to-set-up-mako-templates-with-flask-to-yield-decipherable-exceptions

I am having an issue using flask-mako extension (
https://github.com/tzellman/flask-mako) where the flask debugger page is
not producing useful mako exceptions.

Does anyone have a successful setup with flask & mako templates? Any
help/advice would be appreciated.

Thanks!

- chris

Re: [flask] How to set up mako templates with Flask to yield decipherable exceptions?

From:
Simon Sapin
Date:
2012-05-30 @ 09:38
Le 30/05/2012 02:37, chrickso a écrit :
> Hi everyone,
>
> Please see my stackoverflow post for more details:
> 
http://stackoverflow.com/questions/10807132/how-to-set-up-mako-templates-with-flask-to-yield-decipherable-exceptions
>
> I am having an issue using flask-mako extension
> (https://github.com/tzellman/flask-mako) where the flask debugger page
> is not producing useful mako exceptions.
>
> Does anyone have a successful setup with flask & mako templates? Any
> help/advice would be appreciated.


Hi,

When you have an exception in a template, Jinja itself rewrite Python’s 
traceback object to show the actual Jinja source rather than the 
generated Python code.

Aside: Knowing these details is not required to use Jinja, but Armin 
gave a fascinating talk on the subject of code generation:
http://pyvideo.org/video/641/code-generation-in-python-dismantling-jinja

If you use Jinja by itself, outside of Flask, you will also see the 
rewritten tracebacks. Flask or its debugger do nothing special, Jinja does.

Now as far as I can tell, Mako does not have such traceback rewriting, 
so it will not integrate is nicely with the debugger (or anything based 
on tracebacks.)

Apparently Mako has something completely different to handle exceptions 
more nicely:
http://docs.makotemplates.org/en/latest/usage.html#handling-exceptions

Indeed the Flask-Mako extension could integrate this with the Flask 
debugger (though I’m not sure how exactly), only nobody has developed 
that feature yet.

Regards,
-- 
Simon Sapin

Re: [flask] How to set up mako templates with Flask to yield decipherable exceptions?

From:
chrickso
Date:
2012-05-30 @ 10:17
Yes, looking at the mako exception handling, it looks like it would require
wrapping every response in a try/except call. Is there a place in flask I
would put this code to have it run automatically on each response?

Even with flask-mako installed, it appeared that jinja was still producing
the nice debugger page, it was just missing traceback data specific to mako
so the output was generally unusable if it was a templating error.

All I really need is to get the mako data to the flask debugger somehow...
On May 30, 2012 4:41 AM, "Simon Sapin" <simon.sapin@exyr.org> wrote:

> Le 30/05/2012 02:37, chrickso a écrit :
> > Hi everyone,
> >
> > Please see my stackoverflow post for more details:
> >
> 
http://stackoverflow.com/questions/10807132/how-to-set-up-mako-templates-with-flask-to-yield-decipherable-exceptions
> >
> > I am having an issue using flask-mako extension
> > (https://github.com/tzellman/flask-mako) where the flask debugger page
> > is not producing useful mako exceptions.
> >
> > Does anyone have a successful setup with flask & mako templates? Any
> > help/advice would be appreciated.
>
>
> Hi,
>
> When you have an exception in a template, Jinja itself rewrite Python’s
> traceback object to show the actual Jinja source rather than the
> generated Python code.
>
> Aside: Knowing these details is not required to use Jinja, but Armin
> gave a fascinating talk on the subject of code generation:
> http://pyvideo.org/video/641/code-generation-in-python-dismantling-jinja
>
> If you use Jinja by itself, outside of Flask, you will also see the
> rewritten tracebacks. Flask or its debugger do nothing special, Jinja does.
>
> Now as far as I can tell, Mako does not have such traceback rewriting,
> so it will not integrate is nicely with the debugger (or anything based
> on tracebacks.)
>
> Apparently Mako has something completely different to handle exceptions
> more nicely:
> http://docs.makotemplates.org/en/latest/usage.html#handling-exceptions
>
> Indeed the Flask-Mako extension could integrate this with the Flask
> debugger (though I’m not sure how exactly), only nobody has developed
> that feature yet.
>
> Regards,
> --
> Simon Sapin
>

Re: [flask] How to set up mako templates with Flask to yield decipherable exceptions?

From:
Simon Sapin
Date:
2012-05-30 @ 10:42
Le 30/05/2012 12:17, chrickso a écrit :
> Yes, looking at the mako exception handling, it looks like it would
> require wrapping every response in a try/except call. Is there a place
> in flask I would put this code to have it run automatically on each
> response?

@app.errorhandler(500)
def foo(error):
     do_stuff()
     return 'something', 500

All exceptions that are not handled more specifically should go through 
there.

http://flask.pocoo.org/docs/api/#flask.Flask.errorhandler


> Even with flask-mako installed, it appeared that jinja was still
> producing the nice debugger page, it was just missing traceback data
> specific to mako so the output was generally unusable if it was a
> templating error.

Jinja only rewrites its own traceback frames, and does so even if 
Flask-Mako (or anything) is installed.


> All I really need is to get the mako data to the flask debugger somehow...

I’d say this is possible but not easy. One would have to look in 
Werkzeug’s source code to see how to patch/extend the debugger. Maybe 
Armin could advise on where to start.

-- 
Simon Sapin