The Fix
Refactor `register_error_handler` to consolidate error checking and rewrite some error messages to be more consistent.
Based on closed pallets/flask issue #4559 · PR/commit linked
Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.
@@ -5,6 +5,9 @@ Version 2.2.0
Unreleased
+- Refactor ``register_error_handler`` to consolidate error checking.
+ Rewrite some error messages to be more consistent. :issue:`4559`
+
if isinstance(code_or_exception, HTTPException): # old broken behavior
raise ValueError(
"Tried to register a handler for an exception instance"
f" {code_or_exception!r}. Handlers can only be"
" registered for exception classes or HTTP error codes."
)
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Apply the official fix\nRefactor `register_error_handler` to consolidate error checking and rewrite some error messages to be more consistent.\nWhen NOT to use: This fix is not applicable if the application requires strict HTTPException handling.\n\n
Why This Fix Works in Production
- Trigger: Fix old behavior in `register_error_handler()`
- Mechanism: The code incorrectly raised an error for exception instances instead of allowing all Exception types
- If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.
Why This Breaks in Prod
- The code incorrectly raised an error for exception instances instead of allowing all Exception types
- Production symptom (often without a traceback): Fix old behavior in `register_error_handler()`
Proof / Evidence
- GitHub issue: #4559
- Fix PR: https://github.com/pallets/flask/pull/4560
- Reproduced locally: No (not executed)
- Last verified: 2026-02-12
- Confidence: 0.70
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.63
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“## Background Line 700 src/scaffold.py has a comment # old broken behavior. The purpose of this code is check if the handler is an exception instance. If so, raise an error to warn the user: the handler should be HTTPException classes or HT”
Failure Signature (Search String)
- Fix old behavior in `register_error_handler()`
- [`Line 700 src/scaffold.py`](https://github.com/pallets/flask/blob/f976d5bb88216e921a96998f767df31d7039e4ef/src/flask/scaffold.py#L700) has a comment `# old broken behavior`.
Copy-friendly signature
Failure Signature
-----------------
Fix old behavior in `register_error_handler()`
[`Line 700 src/scaffold.py`](https://github.com/pallets/flask/blob/f976d5bb88216e921a96998f767df31d7039e4ef/src/flask/scaffold.py#L700) has a comment `# old broken behavior`.
Error Message
Signature-only (no traceback captured)
Error Message
-------------
Fix old behavior in `register_error_handler()`
[`Line 700 src/scaffold.py`](https://github.com/pallets/flask/blob/f976d5bb88216e921a96998f767df31d7039e4ef/src/flask/scaffold.py#L700) has a comment `# old broken behavior`.
Minimal Reproduction
if isinstance(code_or_exception, HTTPException): # old broken behavior
raise ValueError(
"Tried to register a handler for an exception instance"
f" {code_or_exception!r}. Handlers can only be"
" registered for exception classes or HTTP error codes."
)
What Broke
Users encountered ValueErrors when trying to register handlers for exception instances.
Why It Broke
The code incorrectly raised an error for exception instances instead of allowing all Exception types
Fix Options (Details)
Option A — Apply the official fix
Refactor `register_error_handler` to consolidate error checking and rewrite some error messages to be more consistent.
Fix reference: https://github.com/pallets/flask/pull/4560
Last verified: 2026-02-12. Validate in your environment.
When NOT to Use This Fix
- This fix is not applicable if the application requires strict HTTPException handling.
Verify Fix
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Did This Fix Work in Your Case?
Quick signal helps us prioritize which fixes to verify and improve.
Prevention
- Capture the exact failing error string in logs and tests so you can reproduce via a minimal script.
- Pin production dependencies and upgrade only with a reproducible test that hits the failing path.
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.