Skip to content

Instantly share code, notes, and snippets.

@wrkhenddher
Created November 1, 2023 14:37
Show Gist options
  • Save wrkhenddher/f96423b6631388868c02d8f68159108e to your computer and use it in GitHub Desktop.
Save wrkhenddher/f96423b6631388868c02d8f68159108e to your computer and use it in GitHub Desktop.
Werkzeug LintMiddleware w/Flask
# Pretty much lifted from https://github.com/pallets/flask/issues/2992
#
# To test:
# $ export FLASK_APP=flask_issue.py
# $ python -m flask run
# $ curl -v http://127.0.0.1:5000/streamingerror
# Notice how respose is 200 but still truncated
from flask import Flask, Response
from werkzeug.middleware.lint import LintMiddleware
app = Flask(__name__)
# Wire LintMiddleware
app.wsgi_app = LintMiddleware(app.wsgi_app)
@app.route('/')
def default_route():
return 'hello world'
@app.route('/error')
def error_route():
raise Exception('error in error_route')
@app.route('/streamingerror')
def streaming_error_route():
def inner_gen():
yield 'hello'
raise Exception('error from streaming_error_route')
yield 'bye'
return Response(inner_gen())
@app.errorhandler(Exception)
def custom_exception_handler(_):
return 'handled by custom exception handler'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment