@@ -59,30 +59,42 @@ def __init__(self, transaction_style="route_name"):
5959 def setup_once ():
6060 # type: () -> None
6161 from pyramid .router import Router # type: ignore
62+ from pyramid .request import Request # type: ignore
6263
6364 old_handle_request = Router .handle_request
6465
6566 def sentry_patched_handle_request (self , request , * args , ** kwargs ):
6667 # type: (Any, Request, *Any, **Any) -> Response
6768 hub = Hub .current
6869 integration = hub .get_integration (PyramidIntegration )
69- if integration is None :
70- return old_handle_request (self , request , * args , ** kwargs )
71-
72- with hub .configure_scope () as scope :
73- scope .add_event_processor (
74- _make_event_processor (weakref .ref (request ), integration )
75- )
70+ if integration is not None :
71+ with hub .configure_scope () as scope :
72+ scope .add_event_processor (
73+ _make_event_processor (weakref .ref (request ), integration )
74+ )
7675
77- try :
78- return old_handle_request (self , request , * args , ** kwargs )
79- except Exception :
80- exc_info = sys .exc_info ()
81- _capture_exception (exc_info )
82- reraise (* exc_info )
76+ return old_handle_request (self , request , * args , ** kwargs )
8377
8478 Router .handle_request = sentry_patched_handle_request
8579
80+ if hasattr (Request , "invoke_exception_view" ):
81+ old_invoke_exception_view = Request .invoke_exception_view
82+
83+ def sentry_patched_invoke_exception_view (self , * args , ** kwargs ):
84+ rv = old_invoke_exception_view (self , * args , ** kwargs )
85+
86+ if (
87+ self .exc_info
88+ and all (self .exc_info )
89+ and rv .status_int == 500
90+ and Hub .current .get_integration (PyramidIntegration ) is not None
91+ ):
92+ _capture_exception (self .exc_info )
93+
94+ return rv
95+
96+ Request .invoke_exception_view = sentry_patched_invoke_exception_view
97+
8698 old_wsgi_call = Router .__call__
8799
88100 def sentry_patched_wsgi_call (self , environ , start_response ):
@@ -92,15 +104,23 @@ def sentry_patched_wsgi_call(self, environ, start_response):
92104 if integration is None :
93105 return old_wsgi_call (self , environ , start_response )
94106
95- return SentryWsgiMiddleware (lambda * a , ** kw : old_wsgi_call (self , * a , ** kw ))(
107+ def sentry_patched_inner_wsgi_call (environ , start_response ):
108+ try :
109+ return old_wsgi_call (self , environ , start_response )
110+ except Exception :
111+ einfo = sys .exc_info ()
112+ _capture_exception (einfo )
113+ reraise (* einfo )
114+
115+ return SentryWsgiMiddleware (sentry_patched_inner_wsgi_call )(
96116 environ , start_response
97117 )
98118
99119 Router .__call__ = sentry_patched_wsgi_call
100120
101121
102- def _capture_exception (exc_info , ** kwargs ):
103- # type: (ExcInfo, **Any ) -> None
122+ def _capture_exception (exc_info ):
123+ # type: (ExcInfo) -> None
104124 if exc_info [0 ] is None or issubclass (exc_info [0 ], HTTPException ):
105125 return
106126 hub = Hub .current
0 commit comments