Skip to content
Myr

Better error message when view return type is not supported

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Before, returning a `bool` from a route caused the error ``` [2019-05-31 10:08:42,216] ERROR in app: Exception on / [GET] Traceback (most recent call last): File "/Users/johnzeringue/Documents/ts-open/flask/flask/app.py", line 2070, in make_response rv = self.response_class.force_type(rv, request.environ) File "/Users/johnzeringue/Documents/ts-open/flask/env/lib/python3.7/site-packages/werkzeug/wrappers/base_response.py", line 269, in force_type response = BaseResponse(*_run_wsgi_app(response, environ)) File "/Users/johnzeringue/Documents/ts-open/flask/env/lib/python3.7/site-packages/werkzeug/wrappers/base_response.py", line 26, in _run_wsgi_app return _run_wsgi_app(*args) File "/Users/johnzeringue/Documents/ts-open/flask/env/lib/python3.7/site-packages/werkzeug/test.py", line 1119, in run_wsgi_app app_rv = app(environ, start_response) TypeError: 'bool' object is not callable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/johnzeringue/Documents/ts-open/flask/flask/app.py", line 2393, in wsgi_app response = self.full_dispatch_request() File "/Users/johnzeringue/Documents/ts-open/flask/flask/app.py", line 1906, in full_dispatch_request return self.finalize_request(rv) File "/Users/johnzeringue/Documents/ts-open/flask/flask/app.py", line 1921, in finalize_request response = self.make_response(rv) File "/Users/johnzeringue/Documents/ts-open/flask/flask/app.py", line 2078, in make_response reraise(TypeError, new_error, sys.exc_info()[2]) File "/Users/johnzeringue/Documents/ts-open/flask/flask/_compat.py", line 39, in reraise raise value.with_traceback(tb) File "/Users/johnzeringue/Documents/ts-open/flask/flask/app.py", line 2070, in make_response rv = self.response_class.force_type(rv, request.environ) File "/Users/johnzeringue/Documents/ts-open/flask/env/lib/python3.7/site-packages/werkzeug/wrappers/base_response.py", line 269, in force_type response = BaseResponse(*_run_wsgi_app(response, environ)) File "/Users/johnzeringue/Documents/ts-open/flask/env/lib/python3.7/site-packages/werkzeug/wrappers/base_response.py", line 26, in _run_wsgi_app return _run_wsgi_app(*args) File "/Users/johnzeringue/Documents/ts-open/flask/env/lib/python3.7/site-packages/werkzeug/test.py", line 1119, in run_wsgi_app app_rv = app(environ, start_response) TypeError: 'bool' object is not callable The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a bool. ``` Now, it returns the more readable ``` [2019-05-31 10:36:19,500] ERROR in app: Exception on / [GET] Traceback (most recent call last): File "/Users/johnzeringue/Documents/ts-open/flask/flask/app.py", line 2400, in wsgi_app response = self.full_dispatch_request() File "/Users/johnzeringue/Documents/ts-open/flask/flask/app.py", line 1907, in full_dispatch_request return self.finalize_request(rv) File "/Users/johnzeringue/Documents/ts-open/flask/flask/app.py", line 1922, in finalize_request response = self.make_response(rv) File "/Users/johnzeringue/Documents/ts-open/flask/flask/app.py", line 2085, in make_response " {rv.__class__.__name__}.".format(rv=rv)) TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a bool. ``` Fixes #3214

authored by John Zeringue · committed by John Zeringue · parent 6ac4b93 · Browse files · 3 files+19 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+19 added−5 removed

  • tests:+7 added−3 removed
  • other:+12 added−2 removed

3 files+19 added−5 removed

modified:flask/app.py+10 added-2 removed
25 unmodified lines
26 MethodNotAllowed,27 default_exceptions,28)29from werkzeug.routing import BuildError, Map, RequestRedirect, RoutingException, Rule30from werkzeug.wrappers import BaseResponse3132from . import cli, json33from ._compat import integer_types, reraise, string_types, text_type34from .config import Config, ConfigAttribute
2028 unmodified lines
2063 rv = self.response_class(rv, status=status, headers=headers)2064 status = headers = None2065 elif isinstance(rv, dict):2066 rv = jsonify(rv)2066 else:2067 elif isinstance(rv, BaseResponse) or callable(rv):2068 # evaluate a WSGI callable, or coerce a different response2069 # class to the correct type2070 try:2071 rv = self.response_class.force_type(rv, request.environ)2072 except TypeError as e:2073 new_error = TypeError(2074 "{e}\nThe view function did not return a valid"2074 " response. The return type must be a string, tuple,"2075 " response. The return type must be a string, dict, tuple,"2076 " Response instance, or WSGI callable, but it was a"2077 " {rv.__class__.__name__}.".format(e=e, rv=rv)2078 )2079 reraise(TypeError, new_error, sys.exc_info()[2])2080 else:2081 raise TypeError(2082 "The view function did not return a valid"2083 " response. The return type must be a string, dict, tuple,"2084 " Response instance, or WSGI callable, but it was a"2085 " {rv.__class__.__name__}.".format(rv=rv)2086 )20872088 # prefer the status if it was provided2089 if status is not None:2090 if isinstance(status, (text_type, bytes, bytearray)):
331 unmodified lines
modified:tests/test_basic.py+7 added-3 removed
1216 unmodified lines
1217 c = app.test_client()12181219 with pytest.raises(TypeError) as e:1220 c.get("/none")1221 assert "returned None" in str(e)12211222 assert "returned None" in str(e)12231224 with pytest.raises(TypeError) as e:1225 c.get("/small_tuple")1225 assert "tuple must have the form" in str(e)12261227 assert "tuple must have the form" in str(e)12281229 pytest.raises(TypeError, c.get, "/large_tuple")12301231 with pytest.raises(TypeError) as e:1232 c.get("/bad_type")1231 assert "it was a bool" in str(e)12331234 assert "object is not callable" not in str(e)1235 assert "it was a bool" in str(e)12361237 pytest.raises(TypeError, c.get, "/bad_wsgi")123812391240def test_make_response(app, req_ctx):
757 unmodified lines
modified:CHANGES.rst+2 added
62 unmodified lines
63 ``flask`` command. :issue:`1357`.64- When using the test client as a context manager (``with client:``),65 all preserved request contexts are popped when the block exits,66 ensuring nested contexts are cleaned up correctly. :pr:`3157`67- Show a better error message when the view return type is not68 supported. :issue:`3214`6970.. _#2935: https://github.com/pallets/flask/issues/293571.. _#2957: https://github.com/pallets/flask/issues/295772.. _#2994: https://github.com/pallets/flask/pull/2994
947 unmodified lines