8000 fix: Do not capture handled exceptions in sanic by abn · Pull Request #127 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

fix: Do not capture handled exceptions in sanic #127

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def sentry_router_get(self, request):
old_error_handler_lookup = ErrorHandler.lookup

def sentry_error_handler_lookup(self, exception):
_capture_exception(exception)
old_error_handler = old_error_handler_lookup(self, exception)

if old_error_handler is None:
_capture_exception(exception)
return None

if Hub.current.get_integration(SanicIntegration) is None:
Expand Down
63 changes: 58 additions & 5 deletions tests/integrations/sanic/test_sanic.py
A615
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sentry_sdk.integrations.sanic import SanicIntegration

from sanic import Sanic, request, response

from sanic.exceptions import InvalidUsage

@pytest.fixture
def app():
Expand Down Expand Up @@ -88,17 +88,70 @@ def myhandler(request, exception):
request, response = app.test_client.get("/error")
assert response.status == 500

event1, event2 = events
assert len(events) == 1

exception, = events[0]["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
assert any(
frame["filename"].endswith("test_sanic.py")
for frame in exception["stacktrace"]["frames"]
)


def test_events_errorhandler_present(sentry_init, app, capture_events):
sentry_init(integrations=[SanicIntegration()])
events = capture_events()

@app.websocket("/error")
def myerror(request, websocket):
return response.json({})

@app.exception(InvalidUsage)
def myhandler(request, exception):
return response.json({}, 400)

exception, = event1["exception"]["values"]
request, r = app.test_client.get("/error")
assert r.status == 400

assert not events


def test_events_errorhandler_absent(sentry_init, app, capture_events):
sentry_init(integrations=[SanicIntegration()])
events = capture_events()

@app.route("/error")
def myerror(request):
raise ValueError('oh no')

request, r = app.test_client.get("/error")
assert r.status == 500

assert len(events) == 1

exception, = events[0]["exception"]["values"]
assert exception["type"] == "ValueError"
assert any(
frame["filename"].endswith("test_sanic.py")
for frame in exception["stacktrace"]["frames"]
)

exception, = event2["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"

def test_events_errorhandler_absent_sanic_error(sentry_init, app, capture_events):
sentry_init(integrations=[SanicIntegration()])
events = capture_events()

@app.route("/error")
def myerror(request):
raise InvalidUsage('oh no')

request, r = app.test_client.get("/error")
assert r.status == 400

assert len(events) == 1

exception, = events[0]["exception"]["values"]
assert exception["type"] == "InvalidUsage"
assert any(
frame["filename"].endswith("test_sanic.py")
for frame in exception["stacktrace"]["frames"]
Expand Down
0