Django-like Error Handling

56 views
Skip to first unread message

Brian Morgan

unread,
May 8, 2012, 10:23:50 AM5/8/12
to pylons-...@googlegroups.com
Please forgive me if this is documented somewhere or has been asked and answered before. I've been searching but haven't been able to find an answer.

How do I get django-like error handling with Pyramid, where I can debug exceptions with the pyramid debugtoolbar during development, and in production send an email and display a nice error message? I've got it mostly working. The problem is that I've defined an error handling view like so:

@view_config(context=Exception, renderer='templates/500.pt')
def catch_500(request):
    response = request.response
    response.status_int = 500
    return {}

And that works great with pyramid_exclog for sending the email. But when I try to run that in development with the pyramid_debugtoolbar included, the exception always gets handled by the view. The behaviour I'd like to see is for the debugtoolbar to catch the exception before it gets to the exception view. When I take out the view definition, the debugtoolbar _will_ catch it, so I know I've got the debugtoolbar included correctly. I'm sure this is not an unusual request, and I've got to be missing something obvious.

Thanks for your help.

-Brian

Chris Lambacher

unread,
May 8, 2012, 4:05:40 PM5/8/12
to pylons-...@googlegroups.com
You can either make it conditionally added using config.add_view in your main function:

from pyramid.config import Configurator
from pyramid.settings import asbool

def main(global_config, **settings):
   config = Configurator(settings=settings)
   if asbool(settings.get('show_friendly_error_page)):
      config.add_view(catch_500, context=Exception, renderer='templates/500.pt')

   return config.make_wsgi_app()


or you can make something that you add via pyramid.includes similar to the debug bar. Knowing how to do that off the top of my head is above my pay grade, but I am sure that you can look at either pyramid_debugtoolbar or the pyramid_exclog to see how to do this. I *think* that you need to is create a module with an includeme functionn as documented for Configureator.include ( http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/api/config.html#pyramid.config.Configurator.include ).

-Chris



-Brian

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pylons-discuss/-/ZnvEgdOxszoJ.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.



--
Christopher Lambacher
ch...@kateandchris.net

Brian Morgan

unread,
May 8, 2012, 5:00:04 PM5/8/12
to pylons-...@googlegroups.com
Chris,

Thanks for taking time to answer my question. It's much appreciated.

-Brian
-Chris

To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to pylons-discuss+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.



--
Christopher Lambacher
ch...@kateandchris.net

Brian Morgan

unread,
May 24, 2012, 10:49:14 AM5/24/12
to pylons-...@googlegroups.com
I found an even simpler solution to this problem. Explicitly specify the tweens, omitting the excview tween in my development settings file. Like so:

pyramid.tweens = 
    pyramid_debugtoolbar.toolbar.toolbar_tween_factory
    pyramid_tm.tm_tween_factory
Reply all
Reply to author
Forward
0 new messages