@@ -59,30 +59,42 @@ def __init__(self, transaction_style="route_name"):
59
59
def setup_once ():
60
60
# type: () -> None
61
61
from pyramid .router import Router # type: ignore
62
+ from pyramid .request import Request # type: ignore
62
63
63
64
old_handle_request = Router .handle_request
64
65
65
66
def sentry_patched_handle_request (self , request , * args , ** kwargs ):
66
67
# type: (Any, Request, *Any, **Any) -> Response
67
68
hub = Hub .current
68
69
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
+ )
76
75
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 )
83
77
84
78
Router .handle_request = sentry_patched_handle_request
85
79
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
+
86
98
old_wsgi_call = Router .__call__
87
99
88
100
def sentry_patched_wsgi_call (self , environ , start_response ):
@@ -92,15 +104,23 @@ def sentry_patched_wsgi_call(self, environ, start_response):
92
104
if integration is None :
93
105
return old_wsgi_call (self , environ , start_response )
94
106
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 )(
96
116
environ , start_response
97
117
)
98
118
99
119
Router .__call__ = sentry_patched_wsgi_call
100
120
101
121
102
- def _capture_exception (exc_info , ** kwargs ):
103
- # type: (ExcInfo, **Any ) -> None
122
+ def _capture_exception (exc_info ):
123
+ # type: (ExcInfo) -> None
104
124
if exc_info [0 ] is None or issubclass (exc_info [0 ], HTTPException ):
105
125
return
106
126
hub = Hub .current
0 commit comments