Skip to content

<spam>#6008

Closed
shr7q wants to merge 1 commit intopallets:mainfrom
shr7q:swe-agent/improve-error-messages-on-simple-after-r-1776900764
Closed

<spam>#6008
shr7q wants to merge 1 commit intopallets:mainfrom
shr7q:swe-agent/improve-error-messages-on-simple-after-r-1776900764

Conversation

@shr7q
Copy link
Copy Markdown

@shr7q shr7q commented Apr 22, 2026

Improve error messages when after_request hooks forget to return response

Summary

This PR adds validation in the process_response method to detect when after_request hooks return None and raise an immediate, descriptive error. Previously, returning None from an after_request hook would silently set the response to None, causing a cryptic TypeError: 'NoneType' object is not callable later during WSGI response delivery. Now users get a clear message identifying the offending hook by name.

Root Cause

The process_response method in src/flask/app.py calls each after_request function and reassigns the response variable to its return value without validation. When a hook returns None (e.g., lambda _: None), the response becomes None. This error only surfaces later in wsgi_app when Flask attempts to call response(environ, start_response), producing an unhelpful error message that doesn't indicate an after_request hook is responsible.

Changes Made

Modified the process_response method in src/flask/app.py to validate the return value of each after_request function. After calling each hook, we now check if the return value is None and raise a TypeError with a descriptive message that includes the hook's function name. This validation applies to all after_request functions across both the app and blueprint scopes.

Testing

All 487 existing tests pass with this change. The fix provides immediate feedback at the point of failure, making debugging significantly easier for users who accidentally omit a return statement in their after_request hooks.

Root cause: In `src/flask/app.py`, the `process_response` method calls each `after_request` function and reassigns the response to its return value without checking whether the return value is `None`. When a hook

Resolved by BugSlayer
@davidism davidism closed this Apr 22, 2026
@davidism davidism changed the title [BugSlayer] Improve error messages on simple after request hook bugs <spam> Apr 23, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 8, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants