Maybe it is better to rewrite the debug mode so the headers are not
sent until iterator completes and if there is error, then return the
correct traceback?


On Dec 16, 10:02 am, Anand Chitipothu <[email protected]> wrote:
> > def wrap(result):
> >    result = iter(result)
> >    try:
> >        while True:
> >            yield result.next()
> >    except Exception, e:
> >         yield str(e) # here we yield Exception
>
> Yielding error might not be the appropriate thing do. Surely,
> displaying error in the log might be useful for debugging.
> Status can't be changed to "500 internal error" because the headers
> will be already sent when exception happens.
>
> In fact, application doesn't cleanup properly when iterator raises
> some exception. It should be fixed.
>
> Here is some code to make iterator safe. Until it is fixed, use
> safegenerator to decorate your GET/POST methods with yield statements.
>
> def safeiter(it, cleanup=None):
>     """Makes given iterator safe by catching any exceptions it raises.
>     If optional cleanup argument is passed, it is called at the end of
> the iteration.
>     """
>     try:
>         for x in it:
>             yield next(it)
>     except StopIteration:
>         pass
>     else:
>         import traceback
>         traceback.print_exc()
>
>     if cleanup:
>         cleanup()
>
> def safegenerator(f):
>     """Decorator to make a generator safe."""
>     def g(*a, **kw):
>         return safeiter(f(*a, **kw))
>     return g

--

You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/webpy?hl=en.


Reply via email to