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

Conversation

abn
Copy link
@abn abn commented Oct 18, 2018

Ignore exceptions with error handlers defined by the sanic application.

Proposing this change in behavior when handling exceptions thrown by a Sanic application as the current implementation triggers issue events for "known" and gracefully handled exceptions and does not allow these to be suppressed at the client side. An example of this, is a case where a client request to a websocket route causes an InvalidHandshake exception which in turn, is handled by sanic to raise an InvalidUsage exception. These exceptions, if a handler is registered explicitly in the application, should not create an issue event as this can lead to a large number of events generated for a heavily used websocket endpoint. Additionally, in any such scenarios an event can be captured by either explicitly using a capture_message or initializing the logging integration and logging an error.

The proposed change retains capturing any errors that may be encountered during the error handler execution.

Ignore exceptions with error handlers defined by the sanic application.
@codecov-io
Copy link
codecov-io commented Oct 18, 2018

Codecov Report

Merging #127 into master will increase coverage by 7.72%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #127      +/-   ##
==========================================
+ Coverage   66.98%   74.71%   +7.72%     
==========================================
  Files          25       25              
  Lines        2484     2167     -317     
  Branches      353      376      +23     
==========================================
- Hits         1664     1619      -45     
+ Misses        687      420     -267     
+ Partials      133      128       -5
Impacted Files Coverage Δ
sentry_sdk/integrations/sanic.py 0% <0%> (-76.07%) ⬇️
sentry_sdk/integrations/aws_lambda.py 0% <0%> (-12.83%) ⬇️
sentry_sdk/_compat.py 84% <0%> (+3.23%) ⬆️
sentry_sdk/hub.py 79.67% <0%> (+4.02%) ⬆️
sentry_sdk/worker.py 87.34% <0%> (+4.2%) ⬆️
sentry_sdk/integrations/flask.py 75.49% <0%> (+4.84%) ⬆️
sentry_sdk/integrations/excepthook.py 86.36% <0%> (+6.36%) ⬆️
sentry_sdk/integrations/django/__init__.py 79.5% <0%> (+7.83%) ⬆️
sentry_sdk/integrations/celery.py 79.48% <0%> (+9.03%) ⬆️
... and 11 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4f67cd4...7480ca6. Read the comment docs.

@untitaker
Copy link
Member

We intentionally did it this way across all framework integrations because an errorhandler for a 500 Internal Server error would suppress all errors.

I would recommend configuring the SDK separately for ignored errors (using before_send) or just muting them in the UI if your plan can afford it

@untitaker untitaker closed this Oct 18, 2018
@abn
Copy link
Author
abn commented Oct 19, 2018

@untitaker I can understand your decision to handle it this way. But, it does feel a bit counter intuitive to consider an error to be not handled, when it has explicitly been done so in the application.

In the case of sanic, when an error handler is defined for SanicException, I would expect all child exceptions to be handled and any exception that does not derive from SanicException to be reported. Which as far as I can tell, does not change with this fix. Verified that with this case (assuming this represents the case you mentioned).

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

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

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

    request, response = app.test_client.get("/error")
    assert response.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"]
    )

Could this behavior be something that can be configured using a flag when initializing the integration? Would appreciate your thoughts on this.

In any case, we might mute for now and use the before_send mechanism as clients hammering endpoints with either bad request methods and/or bad requests is causing a fair amount of noise (~17k events overnight).

@untitaker
Copy link
Member

If I want to have a custom error response for all kinds of 500 Internal Server Errors, I would register a route @app.exception(Exception). I assume most people have a route of this kind. Such a route would swallow up all errors, such that nothing gets sent to sentry.

@untitaker untitaker reopened this Oct 19, 2018
@untitaker untitaker closed this in b50b0b3 Oct 19, 2018
@untitaker
Copy link
Member

@abn I believe I have a good solution for your problem. I suppose what you want is to ignore SanicException in the integration

@abn
Copy link
Author
abn commented Oct 19, 2018

@untitaker for our current issue I think your solution in b50b0b3 would do the trick. Thanks for that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0