Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
70 changes: 56 additions & 14 deletions sentry_sdk/integrations/bottle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
)
from sentry_sdk.integrations._wsgi_common import RequestExtractor
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
from sentry_sdk.tracing import SOURCE_FOR_STYLE
from sentry_sdk.traces import SOURCE_FOR_STYLE as SEGMENT_SOURCE_FOR_STYLE
from sentry_sdk.tracing import SOURCE_FOR_STYLE as TRANSACTION_SOURCE_FOR_STYLE
from sentry_sdk.tracing_utils import has_span_streaming_enabled

Check warning on line 15 in sentry_sdk/integrations/bottle.py

View check run for this annotation

@sentry/warden / warden: code-review

Raised `HTTPResponse` in streaming path captured with `handled=False` instead of `handled=True`

In the span-streaming `wrapped_callback`, when a route handler *raises* an `HTTPResponse` whose status code is in `failed_request_status_codes`, the `except Exception` block calls `_capture_exception(exception, handled=False)`, but `test_span_streaming_failed_request_status_codes` (parametrized with `raise_error=True`) expects `handled=True` — causing those test cases to fail.
Comment thread
ericapisani marked this conversation as resolved.
from sentry_sdk.utils import (
capture_internal_exceptions,
ensure_integration_enabled,
Expand Down Expand Up @@ -100,7 +102,29 @@
scope.add_event_processor(
_make_request_event_processor(self, bottle_request, integration)
)
res = old_handle(self, environ)

if has_span_streaming_enabled(sentry_sdk.get_client().options):
res = old_handle(self, environ)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we can take this line out of the condition bodies since it's the same for both branches

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good eye, will make that fix!


try:
if integration.transaction_style == "url":
name = bottle_request.route.rule or "bottle"
else:
name = (
bottle_request.route.name
or transaction_from_function(bottle_request.route.callback)
or "bottle"
)

sentry_sdk.get_current_scope().set_transaction_name(
name,
source=SEGMENT_SOURCE_FOR_STYLE[integration.transaction_style],
)
except RuntimeError:
pass
Comment thread
ericapisani marked this conversation as resolved.
Outdated

else:
res = old_handle(self, environ)

return res

Expand All @@ -119,17 +143,33 @@
return prepared_callback

def wrapped_callback(*args: object, **kwargs: object) -> "Any":
try:
res = prepared_callback(*args, **kwargs)
except Exception as exception:
_capture_exception(exception, handled=False)
raise exception

if (
isinstance(res, HTTPResponse)
and res.status_code in integration.failed_request_status_codes
):
_capture_exception(res, handled=True)
if has_span_streaming_enabled(sentry_sdk.get_client().options):
with sentry_sdk.traces.start_span(name="bottle"):
Comment thread
ericapisani marked this conversation as resolved.
Outdated
try:
res = prepared_callback(*args, **kwargs)
except Exception as exception:
_capture_exception(exception, handled=False)
raise exception

Check warning on line 152 in sentry_sdk/integrations/bottle.py

View check run for this annotation

@sentry/warden / warden: code-review

[CEJ-M93] Raised `HTTPResponse` in streaming path captured with `handled=False` instead of `handled=True` (additional location)

In the span-streaming `wrapped_callback`, when a route handler *raises* an `HTTPResponse` whose status code is in `failed_request_status_codes`, the `except Exception` block calls `_capture_exception(exception, handled=False)`, but `test_span_streaming_failed_request_status_codes` (parametrized with `raise_error=True`) expects `handled=True` — causing those test cases to fail.

if (
isinstance(res, HTTPResponse)
and res.status_code
in integration.failed_request_status_codes
):
_capture_exception(res, handled=True)

Check warning on line 159 in sentry_sdk/integrations/bottle.py

View check run for this annotation

@sentry/warden / warden: find-bugs

Raised HTTPResponse captured with wrong `handled=False` in streaming path

When a route raises `HTTPResponse` with a status code in `failed_request_status_codes`, the streaming `except` clause captures it with `handled=False`, but the test `test_span_streaming_failed_request_status_codes` asserts `handled=True`; the fix is to check `isinstance(exception, HTTPResponse)` in the except block and use `handled=True` when the status code is in scope.

else:
try:
res = prepared_callback(*args, **kwargs)
except Exception as exception:
_capture_exception(exception, handled=False)
raise exception

if (
isinstance(res, HTTPResponse)
and res.status_code in integration.failed_request_status_codes
):
_capture_exception(res, handled=True)

return res

Expand Down Expand Up @@ -185,7 +225,9 @@
pass

event["transaction"] = name
event["transaction_info"] = {"source": SOURCE_FOR_STYLE[transaction_style]}
event["transaction_info"] = {
"source": TRANSACTION_SOURCE_FOR_STYLE[transaction_style]
}


def _make_request_event_processor(
Expand Down
Loading
Loading