From cf97066080e1ba5f9967f210ce2329545859eee2 Mon Sep 17 00:00:00 2001 From: Gizem Kavas <130026871+kavasg@users.noreply.github.com> Date: Sat, 1 Mar 2025 01:16:11 +0000 Subject: [PATCH] Improve error messages for invalid view function return types --- src/flask/app.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/flask/app.py b/src/flask/app.py index 905b2477..cd9a3ed6 100644 --- a/src/flask/app.py +++ b/src/flask/app.py @@ -1247,11 +1247,18 @@ class Flask(App): ).with_traceback(sys.exc_info()[2]) from None else: raise TypeError( - "The view function did not return a valid" - " response. The return type must be a string," - " dict, list, tuple with headers or status," - " Response instance, or WSGI callable, but it was a" - f" {type(rv).__name__}." + f"The view function for {request.endpoint!r} did not return" + " a valid response.\n\n Valid return types are:\n" + "- str or bytes: converted to a response with the string as body\n" + "- dict or list: converted to a JSON response\n" + "- tuple: either (body, status, headers), (body, status)," + " or (body, headers)\n where body is any of the other types," + " status is an integer, and headers is a dict\n" + "- Response instance: used as-is\n" + "- WSGI callable: called with the WSGI environment\n\n" + f"it was a {type(rv).__name__}.\n See " + "https://flask.palletsprojects.com/en/latest/quickstart/#about-responses" + " for more details." ) rv = t.cast(Response, rv)