Skip to content

Flask integration not capturing exceptions #617

@alexmojaki

Description

@alexmojaki

No matter what I do, exceptions are not appearing in the project.

Run this script from the docs, substituting a DSN:

import sentry_sdk
import flask
from sentry_sdk.integrations.flask import FlaskIntegration

print(flask.__version__)
sentry_sdk.init(
    dsn="https://actual_dsn_here@sentry.io/some_number",
    integrations=[FlaskIntegration()]
)

app = flask.Flask(__name__)


@app.route('/')
def trigger_error():
    division_by_zero = 1 / 0


app.run()

The logs show the exception when visiting the index:

1.1.1
 * Serving Flask app "scratch_540" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
INFO:werkzeug: * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
ERROR:scratch_540:Exception on / [GET]
Traceback (most recent call last):
  File "/home/alex/.cache/pypoetry/virtualenvs/stanbic-trader-solutions-py3.7/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/alex/.cache/pypoetry/virtualenvs/stanbic-trader-solutions-py3.7/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/alex/.cache/pypoetry/virtualenvs/stanbic-trader-solutions-py3.7/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/alex/.cache/pypoetry/virtualenvs/stanbic-trader-solutions-py3.7/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/alex/.cache/pypoetry/virtualenvs/stanbic-trader-solutions-py3.7/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/alex/.cache/pypoetry/virtualenvs/stanbic-trader-solutions-py3.7/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/alex/.PyCharm2019.3/config/scratches/scratch_540.py", line 16, in trigger_error
    division_by_zero = 1 / 0
ZeroDivisionError: division by zero
INFO:werkzeug:127.0.0.1 - - [10/Feb/2020 15:07:23] "GET / HTTP/1.1" 500 -

I think the exception is supposed to be caught here:

try:
rv = self.app(
environ,
functools.partial(
_sentry_start_response, start_response, span
),
)
except BaseException:
reraise(*_capture_exception(hub))

but that code runs without raising an exception at that level, because Flask already caught my exception here: https://github.com/pallets/flask/blob/2c66746a37e52a784b0cf6361cdaef765080538a/src/flask/app.py#L1939-L1945

In fact wsgi._capture_exception isn't called at all.

I'm running the latest versions:

  • sentry-sdk==0.14.1
  • Flask==1.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions