After some more tinkering: the `request` parameter is of type 
*pyramid.util.Request* which is derived from *pyramid.request.Request*, so 
using isinstance() is safe.

As per this example 
<https://docs.pylonsproject.org/projects/pyramid/en/latest/tutorials/wiki2/authentication.html#add-the-authentication-policy>,
 
my request instances have a *user* attribute which is a SQLAlchemy ORM 
instance tied to the request’s db session (see here 
<https://docs.pylonsproject.org/projects/pyramid-cookbook/en/latest/database/sqlalchemy.html#using-a-non-global-session>).
 
The odd thing that I’ve observed in the exception view is the following: 
*sqlalchemy.inspect(request.user).detached* is False, but then 
*request.user.profile* raises an exception.

I have to mention that the exception that enters the exception view is a 
*sqlalchemy.exc.IntegrityError*, i.e. the db session has been rolled back 
at the time when the exception view is entered. Should I check the db 
session’s is_active 
<http://docs.sqlalchemy.org/en/latest/orm/session_api.html#sqlalchemy.orm.session.Session.is_active>
 
attribute inside of the exception view function, i.e:

def handle_exception(exc, request):
    if request.dbsession.is_active:
        # Safe to use request.user.
    else:
        # The exception raised in a view function caused the DB session
        # to flush or roll back--either way it's not usable anymore.

Thanks!


On Saturday, January 27, 2018 at 2:29:00 PM UTC-8, jens.t...@gmail.com 
wrote:
>
> Hello,
>
> I’ve got the following exception view:
>
> @view_config(
>     context=Exception,
>     permission=NO_PERMISSION_REQUIRED,
>     )
> def handle_exception(exc, request):
>     """Last resort view function."""
>     …
>
> I had assumed that the `request` parameter here is the same instance as 
> the `request` parameter from the view function that raised the exception. 
> However, that does not seem to be the case. (In fact, the SQLAlchemy 
> session bound to the view function’s request has been expired by the time 
> the exception view is entered.)
>
> While a normal view function’s `request` parameter is of type 
> *pyramid.request.Request*, the exception view’s is of type 
> *pyramid.util.Request*. Which doesn’t seem to exist though: *AttributeError: 
> module 'pyramid.util' has no attribute 'Request'*
>
> It would be helpful if the docs would shed more light on this. Any more 
> details would be great!
>
> Thanks!
> Jens
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-discuss+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/2f922a5b-a325-4bf3-9bea-81cdf4f974d1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to