question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The function either returned None or ended without a return statement while using with flask_restful

See original GitHub issue

flask_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:closed
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
rycus86commented, Jul 22, 2020

Yeah, good point! In 0.15.4 you’ll be able to do:

app = Flask(__name__)
restful_api = Api(app)

metrics = RESTfulPrometheusMetrics.for_app_factory()
# and later
metrics.init_app(app, restful_api)
1reaction
paurakhsharmacommented, Jul 22, 2020

You are just awesome. Thank you so much this helps me a lot ☺️

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found