Skip to content

Commit d36a591

Browse files
shenekuntitaker
authored andcommitted
Bottle redirect fix (getsentry#317)
Ignore HTTP exceptions that are raised from views
1 parent 62e3a64 commit d36a591

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

sentry_sdk/integrations/bottle.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
from typing import Optional
1919
from bottle import FileUpload, FormsDict, LocalRequest # type: ignore
2020

21-
from bottle import Bottle, Route, request as bottle_request # type: ignore
21+
from bottle import (
22+
Bottle,
23+
Route,
24+
request as bottle_request,
25+
HTTPResponse,
26+
) # type: ignore
2227

2328

2429
class BottleIntegration(Integration):
@@ -94,16 +99,20 @@ def patched_make_callback(self, *args, **kwargs):
9499
return prepared_callback
95100

96101
def wrapped_callback(*args, **kwargs):
97-
try:
98-
res = prepared_callback(*args, **kwargs)
99-
except Exception as exception:
100-
hub = Hub.current
102+
def capture_exception(exception):
101103
event, hint = event_from_exception(
102104
exception,
103105
client_options=hub.client.options,
104106
mechanism={"type": "bottle", "handled": False},
105107
)
106108
hub.capture_event(event, hint=hint)
109+
110+
try:
111+
res = prepared_callback(*args, **kwargs)
112+
except HTTPResponse:
113+
raise
114+
except Exception as exception:
115+
capture_exception(exception)
107116
raise exception
108117

109118
return res

tests/integrations/bottle/test_bottle.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
pytest.importorskip("bottle")
77

88
from io import BytesIO
9-
from bottle import Bottle, debug as set_debug, abort
9+
from bottle import Bottle, debug as set_debug, abort, redirect
1010
from sentry_sdk import capture_message
1111

1212
from sentry_sdk.integrations.logging import LoggingIntegration
@@ -423,3 +423,22 @@ def index():
423423
client.get("/")
424424

425425
assert not events
426+
427+
428+
def test_no_exception_on_redirect(sentry_init, capture_events, app, get_client):
429+
sentry_init(integrations=[bottle_sentry.BottleIntegration()])
430+
events = capture_events()
431+
432+
@app.route("/")
433+
def index():
434+
redirect("/here")
435+
436+
@app.route("/here")
437+
def here():
438+
return "here"
439+
440+
client = get_client()
441+
442+
client.get("/")
443+
444+
assert not events

tox.ini

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ envlist =
1414
{py3.5,py3.6,py3.7}-django-{2.0,2.1}
1515
{pypy,py2.7,py3.5}-django-1.11
1616
{pypy,py2.7,py3.4,py3.5}-django-{1.8,1.9,1.10}
17-
{pypy,py2.7,py3.4,py3.5}-django-1.8
1817
{pypy,py2.7,py3.4}-django-1.7
1918
{pypy,py2.7}-django-1.6
2019

0 commit comments

Comments
 (0)