diff --git a/sentry_sdk/integrations/chalice.py b/sentry_sdk/integrations/chalice.py index ade1c7f10f..e7d2777b53 100644 --- a/sentry_sdk/integrations/chalice.py +++ b/sentry_sdk/integrations/chalice.py @@ -2,7 +2,7 @@ from sentry_sdk._compat import reraise from sentry_sdk.hub import Hub -from sentry_sdk.integrations import Integration +from sentry_sdk.integrations import Integration, DidNotEnable from sentry_sdk.integrations.aws_lambda import _make_request_event_processor from sentry_sdk.utils import ( capture_internal_exceptions, @@ -22,6 +22,11 @@ F = TypeVar("F", bound=Callable[..., Any]) +try: + from chalice import __version__ as CHALICE_VERSION +except ImportError: + raise DidNotEnable("Chalice is not installed") + class EventSourceHandler(ChaliceEventSourceHandler): # type: ignore def __call__(self, event, context): @@ -36,8 +41,7 @@ def __call__(self, event, context): _make_request_event_processor(event, context, configured_time) ) try: - event_obj = self.event_class(event, context) - return self.func(event_obj) + return ChaliceEventSourceHandler.__call__(self, event, context) except Exception: exc_info = sys.exc_info() event, hint = event_from_exception( @@ -92,7 +96,18 @@ class ChaliceIntegration(Integration): @staticmethod def setup_once(): # type: () -> None - old_get_view_function_response = Chalice._get_view_function_response + try: + version = tuple(map(int, CHALICE_VERSION.split(".")[:3])) + except (ValueError, TypeError): + raise DidNotEnable("Unparsable Chalice version: {}".format(CHALICE_VERSION)) + if version < (1, 20): + old_get_view_function_response = Chalice._get_view_function_response + else: + from chalice.app import RestAPIEventHandler + + old_get_view_function_response = ( + RestAPIEventHandler._get_view_function_response + ) def sentry_event_response(app, view_function, function_args): # type: (Any, F, **Any) -> Any @@ -104,6 +119,9 @@ def sentry_event_response(app, view_function, function_args): app, wrapped_view_function, function_args ) - Chalice._get_view_function_response = sentry_event_response + if version < (1, 20): + Chalice._get_view_function_response = sentry_event_response + else: + RestAPIEventHandler._get_view_function_response = sentry_event_response # for everything else (like events) chalice.app.EventSourceHandler = EventSourceHandler diff --git a/tox.ini b/tox.ini index eb85a4b654..331dc0c192 100644 --- a/tox.ini +++ b/tox.ini @@ -77,7 +77,7 @@ envlist = {py3.5,py3.6,py3.7,py3.8,py3.9}-pure_eval - {py3.6,py3.7,py3.8}-chalice-{1.16,1.17,1.18,1.19} + {py3.6,py3.7,py3.8}-chalice-{1.16,1.17,1.18,1.19,1.20} [testenv] deps = @@ -204,6 +204,7 @@ deps = chalice-1.17: chalice>=1.17.0,<1.18.0 chalice-1.18: chalice>=1.18.0,<1.19.0 chalice-1.19: chalice>=1.19.0,<1.20.0 + chalice-1.20: chalice>=1.20.0,<1.21.0 chalice: pytest-chalice==0.0.5 setenv =