Steve Piercy
2018-09-22 2a45fe74f9598b4e726ab17ce17948d4e709894b
commit | author | age
131132 1 import sys
0c1c39 2
e5561f 3 from pyramid.compat import reraise
3b886e 4 from pyramid.httpexceptions import HTTPNotFound
131132 5
793621 6 def _error_handler(request, exc):
MM 7     # NOTE: we do not need to delete exc_info because this function
8     # should never be in the call stack of the exception
9     exc_info = sys.exc_info()
10
11     try:
3b886e 12         response = request.invoke_exception_view(exc_info)
MM 13     except HTTPNotFound:
14         # re-raise the original exception as no exception views were
15         # able to handle the error
793621 16         reraise(*exc_info)
MM 17
18     return response
19
131132 20 def excview_tween_factory(handler, registry):
CM 21     """ A :term:`tween` factory which produces a tween that catches an
22     exception raised by downstream tweens (or the main Pyramid request
23     handler) and, if possible, converts it into a Response using an
e2e51b 24     :term:`exception view`.
MM 25
26     .. versionchanged:: 1.9
27        The ``request.response`` will be remain unchanged even if the tween
28        handles an exception. Previously it was deleted after handling an
29        exception.
30
31        Also, ``request.exception`` and ``request.exc_info`` are only set if
32        the tween handles an exception and returns a response otherwise they
33        are left at their original values.
34
35     """
131132 36
CM 37     def excview_tween(request):
38         try:
8d1533 39             response = handler(request)
44ebac 40         except Exception as exc:
793621 41             response = _error_handler(request, exc)
8d1533 42         return response
131132 43
CM 44     return excview_tween
45
fd78f1 46 MAIN = 'MAIN'
CM 47 INGRESS = 'INGRESS'
4f070b 48 EXCVIEW = 'pyramid.tweens.excview_tween_factory'