|
| 1 | +from collections.abc import Set |
1 | 2 | import sentry_sdk |
2 | 3 | from sentry_sdk.consts import OP |
3 | | -from sentry_sdk.integrations import DidNotEnable, Integration |
| 4 | +from sentry_sdk.integrations import _DEFAULT_FAILED_REQUEST_STATUS_CODES, DidNotEnable, Integration |
4 | 5 | from sentry_sdk.integrations.asgi import SentryAsgiMiddleware |
5 | 6 | from sentry_sdk.integrations.logging import ignore_logger |
6 | 7 | from sentry_sdk.scope import should_send_default_pii |
|
17 | 18 | from litestar.middleware import DefineMiddleware # type: ignore |
18 | 19 | from litestar.routes.http import HTTPRoute # type: ignore |
19 | 20 | from litestar.data_extractors import ConnectionDataExtractor # type: ignore |
| 21 | + from litestar.exceptions import HTTPException # type: ignore |
20 | 22 | except ImportError: |
21 | 23 | raise DidNotEnable("Litestar is not installed") |
22 | 24 |
|
@@ -45,6 +47,11 @@ class LitestarIntegration(Integration): |
45 | 47 | identifier = "litestar" |
46 | 48 | origin = f"auto.http.{identifier}" |
47 | 49 |
|
| 50 | + def __init__(self, |
| 51 | + failed_request_status_codes=_DEFAULT_FAILED_REQUEST_STATUS_CODES, # type: Set[int] |
| 52 | + ) -> None: |
| 53 | + self.failed_request_status_codes = failed_request_status_codes |
| 54 | + |
48 | 55 | @staticmethod |
49 | 56 | def setup_once(): |
50 | 57 | # type: () -> None |
@@ -277,6 +284,13 @@ def exception_handler(exc, scope): |
277 | 284 | sentry_scope = sentry_sdk.get_isolation_scope() |
278 | 285 | sentry_scope.set_user(user_info) |
279 | 286 |
|
| 287 | + if isinstance(exc, HTTPException): |
| 288 | + integration = sentry_sdk.get_client().get_integration( |
| 289 | + LitestarIntegration |
| 290 | + ) |
| 291 | + if integration is not None and exc.status_code not in integration.failed_request_status_codes: |
| 292 | + return |
| 293 | + |
280 | 294 | event, hint = event_from_exception( |
281 | 295 | exc, |
282 | 296 | client_options=sentry_sdk.get_client().options, |
|
0 commit comments