From 41c3dcc62f090f47e53eb0f3261006288c6f7c55 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 22 Jul 2022 16:39:56 +0200 Subject: [PATCH 1/5] Fixed problem with broken response --- sentry_sdk/integrations/starlette.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index 5fa8719e75..3df3c0fb30 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -423,6 +423,7 @@ async def __call__(self, scope, receive, send): hub = Hub.current integration = hub.get_integration(StarletteIntegration) if integration is None: + await self.app(scope, receive, send) return with hub.configure_scope() as sentry_scope: From f630eaa61fdf3add1c6085f5eec382b0a990da28 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 22 Jul 2022 16:42:07 +0200 Subject: [PATCH 2/5] Same fix for fastapi --- sentry_sdk/integrations/fastapi.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sentry_sdk/integrations/fastapi.py b/sentry_sdk/integrations/fastapi.py index c5fa4e84e2..2ec4800b19 100644 --- a/sentry_sdk/integrations/fastapi.py +++ b/sentry_sdk/integrations/fastapi.py @@ -96,6 +96,7 @@ async def __call__(self, scope, receive, send): hub = Hub.current integration = hub.get_integration(FastApiIntegration) if integration is None: + await self.app(scope, receive, send) return with hub.configure_scope() as sentry_scope: From 68eb2ee7d4640b64b41dfe4bbdee13ee5d966a55 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 22 Jul 2022 17:34:57 +0200 Subject: [PATCH 3/5] Fix problem when python-multipart is not installed --- sentry_sdk/integrations/starlette.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index 3df3c0fb30..09c965e6b7 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -339,7 +339,13 @@ async def form(self): curl -X POST http://localhost:8000/upload/somethign -H "Content-Type: application/x-www-form-urlencoded" -d "username=kevin&password=welcome123" curl -X POST http://localhost:8000/upload/somethign -F username=Julian -F password=hello123 """ - return await self.request.form() + try: + return await self.request.form() + + except AssertionError: + # We can not get form information, + # because `python-multipart` is not installed. + return def is_json(self): # type: (StarletteRequestExtractor) -> bool From 5c6c2e45661b86cffa7aeadcb23037d87bac7d06 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 22 Jul 2022 18:02:14 +0200 Subject: [PATCH 4/5] Refactoring to make it like in other places in the sdk --- sentry_sdk/integrations/starlette.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index 09c965e6b7..ac263e732e 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -1,6 +1,5 @@ from __future__ import absolute_import - from sentry_sdk._compat import iteritems from sentry_sdk._types import MYPY from sentry_sdk.hub import Hub, _should_send_default_pii @@ -340,13 +339,13 @@ async def form(self): curl -X POST http://localhost:8000/upload/somethign -F username=Julian -F password=hello123 """ try: - return await self.request.form() - - except AssertionError: - # We can not get form information, - # because `python-multipart` is not installed. + # Optional dependency of Starlette to parse form data. + import multipart # type: ignore # noqa: F401 + except ImportError: return + return await self.request.form() + def is_json(self): # type: (StarletteRequestExtractor) -> bool return _is_json_content_type(self.request.headers.get("content-type")) From c4f52f536b77adc18a7def80c9be35865efe5a73 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 22 Jul 2022 18:14:35 +0200 Subject: [PATCH 5/5] Refactoring --- sentry_sdk/integrations/starlette.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index ac263e732e..e2c5366ae2 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -40,6 +40,12 @@ # Startlette 0.19.1 from starlette.exceptions import ExceptionMiddleware # type: ignore +try: + # Optional dependency of Starlette to parse form data. + import multipart # type: ignore # noqa: F401 +except ImportError: + multipart = None + _DEFAULT_TRANSACTION_NAME = "generic Starlette request" @@ -338,11 +344,8 @@ async def form(self): curl -X POST http://localhost:8000/upload/somethign -H "Content-Type: application/x-www-form-urlencoded" -d "username=kevin&password=welcome123" curl -X POST http://localhost:8000/upload/somethign -F username=Julian -F password=hello123 """ - try: - # Optional dependency of Starlette to parse form data. - import multipart # type: ignore # noqa: F401 - except ImportError: - return + if multipart is None: + return None return await self.request.form()