Jump to solution
Verify

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%.

Jump to Verify Open PR/Commit
@@ -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` +
repro.py
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." )
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
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
Production impact:
  • 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

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”
Issue thread · issue description · source

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
signature.txt
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.txt
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

repro.py
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.

When NOT to use: This fix is not applicable if the application requires strict HTTPException handling.

Fix reference: https://github.com/pallets/flask/pull/4560

Last verified: 2026-02-12. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix is not applicable if the application requires strict HTTPException handling.

Verify Fix

verify
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.