Skip to content
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

Sentry raising BadRequest in Flask 2.1.2 #1459

Closed
antonpirker opened this issue Jun 8, 2022 · 4 comments
Closed

Sentry raising BadRequest in Flask 2.1.2 #1459

antonpirker opened this issue Jun 8, 2022 · 4 comments
Labels
Integration: Flask Type: Bug Something isn't working

Comments

@antonpirker
Copy link
Member

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.5.12

Steps to Reproduce

See also internal ticket here: https://getsentry.atlassian.net/jira/software/c/projects/WEBBACKEND/boards/150?modal=detail&selectedIssue=ISSUE-1506&quickFilter=260

Create a sample application with Flask 2.1.2 like this:

from flask import Flask
import sentry_sdk

from sentry_sdk.integrations.flask import FlaskIntegration

sentry_sdk.init(
    dsn="https://d767560f70c3498ba64a91a16d987514@o447951.ingest.sentry.io/6483053",
    integrations=[FlaskIntegration()],
    debug=True,

    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for performance monitoring.
    # We recommend adjusting this value in production.
#    traces_sample_rate=1.0
)


app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

@app.route('/debug-sentry')
def trigger_error():
    division_by_zero = 1 / 0
    return "<p>bla</p>"

put this in hello.py and start with FLASK_APP=hello flask run.

Expected Result

The errors should be caught and sent to Sentry.

Actual Result

There is a BadRequest Error during processing of the Error. (But the error is still sent to Sentry, but this BadRequest should not be there):

 [sentry] ERROR: Internal error in sentry_sdk
Traceback (most recent call last):
  File "/Users/antonpirker/tmp/flask/.venv/lib/python3.9/site-packages/flask/wrappers.py", line 128, in on_json_loading_failed
    return super().on_json_loading_failed(e)
  File "/Users/antonpirker/tmp/flask/.venv/lib/python3.9/site-packages/werkzeug/wrappers/request.py", line 611, in on_json_loading_failed
    raise BadRequest(
werkzeug.exceptions.BadRequest: 400 Bad Request: Did not attempt to load JSON data because the request Content-Type was not 'application/json'.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/antonpirker/tmp/flask/.venv/lib/python3.9/site-packages/sentry_sdk/integrations/flask.py", line 192, in inner
    FlaskRequestExtractor(request).extract_into_event(event)
  File "/Users/antonpirker/tmp/flask/.venv/lib/python3.9/site-packages/sentry_sdk/integrations/_wsgi_common.py", line 72, in extract_into_event
    parsed_body = self.parsed_body()
  File "/Users/antonpirker/tmp/flask/.venv/lib/python3.9/site-packages/sentry_sdk/integrations/_wsgi_common.py", line 121, in parsed_body
    return self.json()
  File "/Users/antonpirker/tmp/flask/.venv/lib/python3.9/site-packages/sentry_sdk/integrations/flask.py", line 172, in json
    return self.request.get_json()
  File "/Users/antonpirker/tmp/flask/.venv/lib/python3.9/site-packages/werkzeug/wrappers/request.py", line 571, in get_json
    return self.on_json_loading_failed(None)
  File "/Users/antonpirker/tmp/flask/.venv/lib/python3.9/site-packages/flask/wrappers.py", line 133, in on_json_loading_failed
    raise BadRequest() from e
werkzeug.exceptions.BadRequest: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
 [sentry] INFO: Discarded session update because of missing release

When you downgrade some of the requirements of Flask, it works:

Flask              2.1.2
itsdangerous       2.1.2
Jinja2             3.1.2
MarkupSafe         2.1.1
Werkzeug           2.0.3
@hitvoice
Copy link

I had the same issue without using sentry. It worked after downgrading werkzeug. I think it's a bug from there.

@azzamsa
Copy link

azzamsa commented Oct 26, 2022

@azzamsa
Copy link

azzamsa commented Oct 26, 2022

I managed to solve the issue. I put the full explanation in my StackOverflow answer

@antonpirker
Copy link
Member Author

As this is no Sentry bug, I am closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Integration: Flask Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants