8000 fix(chalice): Enable support for Chalice 1.20 by Gleekzone · Pull Request #832 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

fix(chalice): Enable support for Chalice 1.20 #832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions sentry_sdk/integrations/chalice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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 =
Expand Down
0