Skip to content

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

@paurakhsharma

Description

@paurakhsharma

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions