Merge pull request #3235 from johnzeringue/better-return-type-error
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Better error message when view return type is not supported
authored by David Lord · committed by GitHub · parents 14e9291, 8bb7185 · 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
Provenance
- Commit
52f9aa7- Tree
42958f1- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
14e9291- Parent 2
8bb7185
3 files changed+19 added−5 removed
modified:flask/app.py+10 added-2 removed
25 unmodified lines26 MethodNotAllowed,27 default_exceptions,28 )29 from werkzeug.routing import BuildError, Map, RequestRedirect, RoutingException, Rule30+from werkzeug.wrappers import BaseResponse31 32 from . import cli, json33 from ._compat import integer_types, reraise, string_types, text_type34 from .config import Config, ConfigAttribute2028 unmodified lines2063 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+ )2087 2088 # prefer the status if it was provided2089 if status is not None:2090 if isinstance(status, (text_type, bytes, bytearray)):331 unmodified linesmodified:tests/test_basic.py+7 added-3 removed
1216 unmodified lines1217 c = app.test_client()1218 1219 with pytest.raises(TypeError) as e:1220 c.get("/none")1221- assert "returned None" in str(e)1221 1222+ assert "returned None" in str(e)1223+1224 with pytest.raises(TypeError) as e:1225 c.get("/small_tuple")1225- assert "tuple must have the form" in str(e)1226 1227+ assert "tuple must have the form" in str(e)1228+1229 pytest.raises(TypeError, c.get, "/large_tuple")1230 1231 with pytest.raises(TypeError) as e:1232 c.get("/bad_type")1231- assert "it was a bool" in str(e)1233 1234+ assert "object is not callable" not in str(e)1235+ assert "it was a bool" in str(e)1236+1237 pytest.raises(TypeError, c.get, "/bad_wsgi")1238 1239 1240 def test_make_response(app, req_ctx):757 unmodified linesmodified:CHANGES.rst+2 added
62 unmodified lines63 ``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`69 70 .. _#2935: https://github.com/pallets/flask/issues/293571 .. _#2957: https://github.com/pallets/flask/issues/295772 .. _#2994: https://github.com/pallets/flask/pull/2994947 unmodified lines