2

Here is part of app.py:

app = Flask(__name__)
app.config.from_object('SETTINGS')
app.wsgi_app = Middleware(app.wsgi_app, app)


@app.errorhandler(ValueError)
def all_exception_handler(error):
    print('Error Catched!')
    return 'Error Handler', 500

And here is part of Middleware class:

class Middleware:
    def __init__(self, wsgi, app):
        self.wsgi = wsgi
        self.app = app


    def __call__(self, environ, start_response):

        raise ValueError('Value Error')

        return self.wsgi(environ, start_response)

So my issue is when I raise ValueError exception inside Middleware (raise ValueError('Value Error') the @app.errorhandler(ValueError) is not triggering thus I'm not receiving print('Error Catched!') in my console

Kadiem Alqazzaz
  • 554
  • 1
  • 7
  • 22

1 Answers1

2

Hello again my friend

I told you that Middlewares is out of app layer(outside of the request / outside of the Flask application etc) => application error handler will not work. Just move your logic into @app.before_request or app.before_request_funcs

Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
  • Maybe you did not understand my previous answer (but I asked if it was clear). Just use `@app.before_request` + `@app.errorhandler` + `flask.redirect()` – Danila Ganchar Jul 27 '21 at 12:23
  • Yh I got it that's why I started the project again but with Django much more easier anyways thanks for you help – Kadiem Alqazzaz Jul 27 '21 at 12:42
  • @KadiemAlqazzaz I understand ... `Django Middlewares` is a little different and understandable. Anyway feel free to ask a details in comments. good luck! – Danila Ganchar Jul 27 '21 at 12:46