Flask's abort(404) does not work with Zero
Hubro opened this issue · comments
I've made a Python API file for fetching a resource. If the resource doesn't exist, I want to return "404 Not Found". I have tried Flask's abort(404)
, but the NotFound error is caught at some point and I get a TypeError instead, resulting in a 500 Server Error from Zero. Here is the stack trace:
werkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. [2020-06-05 20:40:01,549] ERROR in app: Exception on /video/foobar/ [GET] Traceback (most recent call last): File "/home/tomas/.local/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "/home/tomas/.local/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request return self.finalize_request(rv) File "/home/tomas/.local/lib/python3.8/site-packages/flask/app.py", line 1967, in finalize_request response = self.make_response(rv) File "/home/tomas/.local/lib/python3.8/site-packages/flask/app.py", line 2096, in make_response raise TypeError( TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
Is this a bug? Or is there a different way to do it?
And as usual, I finally figured it out myself right after posting the question:
from flask import make_response
def handler(name):
if not VIDEO_PATH.joinpath(name).exists():
return make_response("", 404)
# ...
I still think abort(404)
should work though, so I'll keep this open as a feature request / bug report.