I'm not sure if this is a bug or intentional behaviour, but I've had trouble accessing exception information in response and finished callbacks.
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def error(request):
raise Exception('this is an error message')
def response_callback(request, response):
print 'response', request.path_qs, response.status_int
def finished_callback(request):
print 'finished', request.path_qs, request.exception, request.response.status_int
def on_new_request(event):
request = event.request
request.add_response_callback(response_callback)
request.add_finished_callback(finished_callback)
if __name__ == '__main__':
config = Configurator()
config.add_route('error', '/error')
config.add_view(error, route_name='error')
config.add_subscriber(on_new_request,
'pyramid.events.NewRequest')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
#----
It looks like the exception view tween deliberately sets the request.exception attribute to None. Does this mean that the documentation for response and finished callbacks should be updated to not refer to that attribute?
Also, is there any other way of detecting whether a view raised an exception in one of these callbacks?