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