On Thu, Jan 06, 2011 at 04:04:06PM -0800, Justin Francis wrote:
> I have recently started using Pylons and am having trouble finding
> information about the exact structure of the error handling stack.
> When an exception occurs anywhere, what is the sequence of calls that
> Pylons makes (from the time the exeception occurs until the error
> screen is shown)? How does this sequence differ between debug mode and
> release mode?

That's an excellent question.  I've had this exact wish (figure out the
description of Pylons error handling) for a long time now in my
todo-list.

> This leads me to my question: I want to do a custom cleanup right
> before Pylons displays the error page. I have some database objects on
> which I want to issue a Rollback(). Where in the Pylons code would I
> place this rollback, so that it gets called whenever Pylons handles an
> exception?

Traditionally you'd do this in BaseController.__call__:

    def __call__(self, environ, start_response):
        try:
            return WSGIController.__call__(self, environ, start_response)
        except:
            # do your error cleanup
            raise
        finally:
            # do your cleanup that's suitable for both errors and normal
            # requests, e.g. sqlalchemy's
            meta.Session.remove()

Marius Gedminas
-- 
One could envision a different approach to persistence (hands wave and
magical stardust appears overhead to percussive indian string music)
where objects in the database were proxied rather than deriving from a
common base class.
        -- Casey Duncan

Attachment: signature.asc
Description: Digital signature

Reply via email to