Sentry raising BadRequest in Flask 2.1.2
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://[email protected]/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
I had the same issue without using sentry. It worked after downgrading werkzeug. I think it's a bug from there.
@hitvoice what werkzeug version works for you?
Related:
- https://github.com/flask-restful/flask-restful/issues/953
- https://github.com/pallets/werkzeug/commit/bf4ede275335644102cd72c421c2b6976bc69883
I managed to solve the issue. I put the full explanation in my StackOverflow answer
As this is no Sentry bug, I am closing this.