Bird
0
0

Why does this middleware cause an error?

medium📝 Debug Q7 of 15
Flask - Middleware and Extensions
Why does this middleware cause an error?
class ErrorMiddleware:
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        start_response('200 OK')
        return self.app(environ, start_response)

app.wsgi_app = ErrorMiddleware(app.wsgi_app)
AMissing return statement in __call__
Bapp.wsgi_app not wrapped properly
CMiddleware class missing __init__ method
Dstart_response called twice causing protocol error
Step-by-Step Solution
Solution:
  1. Step 1: Understand start_response usage

    start_response must be called once per request to set headers.
  2. Step 2: Identify double call

    Middleware calls start_response, then wrapped app calls it again causing error.
  3. Final Answer:

    start_response called twice causing protocol error -> Option D
  4. Quick Check:

    Call start_response only once per request [OK]
Quick Trick: Call start_response only once in middleware chain [OK]
Common Mistakes:
MISTAKES
  • Calling start_response before wrapped app
  • Missing return statement
  • Not wrapping app.wsgi_app

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes