The function either returned None or ended without a return statement while using with flask_restful
See original GitHub issueflask_restful
supports returning None
from the view function, but I am getting error this error when I use metrics
for endpoint.
e.g
from flask_restful import Resource
by_path_counter = metrics.counter(
'by_path_counter', 'Request count by request paths',
labels={'path': lambda: request.path}
)
class FoodApi(Resource):
@by_path_counter
def post(self):
create_food()
return None, 200
It works fine if I don’t use @by_path_counter
decorator, and returns null
in the response.
But when I add the decorator, I get this error.
Traceback (most recent call last):
File "/home/wartner/.local/share/virtualenvs/config_service-TDer8cy_/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/wartner/.local/share/virtualenvs/config_service-TDer8cy_/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/wartner/.local/share/virtualenvs/config_service-TDer8cy_/lib/python3.8/site-packages/flask_restful/__init__.py", line 468, in wrapper
resp = resource(*args, **kwargs)
File "/home/wartner/.local/share/virtualenvs/config_service-TDer8cy_/lib/python3.8/site-packages/flask/views.py", line 89, in view
return self.dispatch_request(*args, **kwargs)
File "/home/wartner/.local/share/virtualenvs/config_service-TDer8cy_/lib/python3.8/site-packages/flask_restful/__init__.py", line 583, in dispatch_request
resp = meth(*args, **kwargs)
File "/home/wartner/.local/share/virtualenvs/config_service-TDer8cy_/lib/python3.8/site-packages/prometheus_flask_exporter/__init__.py", line 633, in func
response = make_response(response)
File "/home/wartner/.local/share/virtualenvs/config_service-TDer8cy_/lib/python3.8/site-packages/flask/helpers.py", line 223, in make_response
return current_app.make_response(args)
File "/home/wartner/.local/share/virtualenvs/config_service-TDer8cy_/lib/python3.8/site-packages/flask/app.py", line 2097, 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 there any way, I can work around this? (I have to return None
)
Any kind of help will be highly appreciated, thank you 🙂
Note: It works fine on other view functions where I return not None
values
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Flask TypeError : The view function did not return a valid ...
The problem here is that one of your functions returns None and not that a return statement is missing, as observed in the...
Read more >[SOLVED] ValueError in Flask: View function did not return a ...
This will return none as the output. We can avoid ValueError: View function did not return a response by returning none value. Both...
Read more >What does 'TypeError: The view function did not return a valid ...
The view function must return a response. Returning None, or the view ending without returning, is not allowed. // This is the reason...
Read more >ValueError: View Function Did Not Return a Response - Sentry
The Solution All Flask views must return a value, usually a Response object. If the flask view throwing the error accepts GET and...
Read more >Extending Flask-RESTful — Flask-RESTful 0.3.8 documentation
These representation functions must return a Flask Response object. ... If the application is running in debug mode ( app.debug = True )...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yeah, good point! In
0.15.4
you’ll be able to do:You are just awesome. Thank you so much this helps me a lot ☺️