22
33from sentry_sdk ._compat import reraise
44from sentry_sdk .hub import Hub
5- from sentry_sdk .integrations import Integration
5+ from sentry_sdk .integrations import Integration , DidNotEnable
66from sentry_sdk .integrations .aws_lambda import _make_request_event_processor
77from sentry_sdk .utils import (
88 capture_internal_exceptions ,
2222
2323 F = TypeVar ("F" , bound = Callable [..., Any ])
2424
25+ try :
26+ from chalice import __version__ as CHALICE_VERSION
27+ except ImportError :
28+ raise DidNotEnable ("Chalice is not installed" )
29+
2530
2631class EventSourceHandler (ChaliceEventSourceHandler ): # type: ignore
2732 def __call__ (self , event , context ):
@@ -36,8 +41,7 @@ def __call__(self, event, context):
3641 _make_request_event_processor (event , context , configured_time )
3742 )
3843 try :
39- event_obj = self .event_class (event , context )
40- return self .func (event_obj )
44+ return ChaliceEventSourceHandler .__call__ (self , event , context )
4145 except Exception :
4246 exc_info = sys .exc_info ()
4347 event , hint = event_from_exception (
@@ -92,7 +96,18 @@ class ChaliceIntegration(Integration):
9296 @staticmethod
9397 def setup_once ():
9498 # type: () -> None
95- old_get_view_function_response = Chalice ._get_view_function_response
99+ try :
100+ version = tuple (map (int , CHALICE_VERSION .split ("." )[:3 ]))
101+ except (ValueError , TypeError ):
102+ raise DidNotEnable ("Unparsable Chalice version: {}" .format (CHALICE_VERSION ))
103+ if version < (1 , 20 ):
104+ old_get_view_function_response = Chalice ._get_view_function_response
105+ else :
106+ from chalice .app import RestAPIEventHandler
107+
108+ old_get_view_function_response = (
109+ RestAPIEventHandler ._get_view_function_response
110+ )
96111
97112 def sentry_event_response (app , view_function , function_args ):
98113 # type: (Any, F, **Any) -> Any
@@ -104,6 +119,9 @@ def sentry_event_response(app, view_function, function_args):
104119 app , wrapped_view_function , function_args
105120 )
106121
107- Chalice ._get_view_function_response = sentry_event_response
122+ if version < (1 , 20 ):
123+ Chalice ._get_view_function_response = sentry_event_response
124+ else :
125+ RestAPIEventHandler ._get_view_function_response = sentry_event_response
108126 # for everything else (like events)
109127 chalice .app .EventSourceHandler = EventSourceHandler
0 commit comments