简体   繁体   中英

How to handle a flask endpoint call when function does not return anything

I am working on dynamically reading all python method and generating endpoints from flask.But I am seeing below exception

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2464, in __call__ return self.wsgi_app(environ, start_response) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2450, in wsgi_app response = self.handle_exception(e) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise raise value File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1953, in full_dispatch_request return self.finalize_request(rv) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1968, in finalize_request response = self.make_response(rv) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2098, in make_response "The view function did not return a valid response. The" TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

This is happening because the method is not returning anything which is not acceptable in flask, is there a way to handle this situation where the underlying python method return type can be handled separately. I checked make_response in flask but that too does not serve the needs.

Basically i had to create MyFlask class which has Flask as superclass.

class MyFlask(Flask):
def make_response(self, rv):
    if not rv:
        return super().make_response("Any String")
    return super().make_response(rv) 

app = MyFlask(__name__)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM