Skip to content

refactor: every raise inside except chains with from - #25

Merged
JsNcAr merged 2 commits into
mainfrom
refactor/exception-chaining
Oct 15, 2026
Merged

JsNcAr merged 2 commits into
mainfrom
refactor/exception-chaining

Conversation

@JsNcAr

@JsNcAr JsNcAr commented Oct 15, 2026

Copy link
Copy Markdown
Owner

The second of the two enhancements from yesterday's audit. Branched from ci/warnings-are-errors (#24) so the changelog entries do not conflict; merge #24 first.

Why

A raise inside an except block without from leaves Python to guess the relation between the two errors; the traceback then says "During handling of the above exception, another exception occurred", which is wrong for a deliberate translation and noisy for an error that was already logged. PEP 3134 chaining states it. ruff's bugbear rule B904 found 13 such sites; it now finds none.

Changes

  • from e, the new error translates the caught one: the integer and log-format parsers and the algorithm lookup in config.py, the rate-limit syntax check in limiter.py, the TOML read in scripts/release.py, the profile URL validator in schemas/user.py, IntegrityError to IdentifierTakenError in services/users.py (twice), PyJWT errors to the 401 in auth.py.
  • from None, the cause is already logged and must not reach the client: the four generic 500 responses in routers/users.py, each preceded by logger.exception.
  • AGENTS.md Errors rule gains the convention; changelog entry (refactor: every raise inside except chains with from #25).

No response body or status changes.

Verification, local on Python 3.15.0 with PostgreSQL 17

black, flake8, mypy clean; ruff check --select B904 reports nothing (run once with uvx, not added to the project); the exact CI command from #24 (python -X warn_default_encoding -m pytest): 140 passed, no warnings. Sanity: minimum_secret_bytes("none") raises a ValueError whose __cause__ is the KeyError.

🤖 Generated with Claude Code

JsNcAr and others added 2 commits October 15, 2026 10:48
pytest.ini sets filterwarnings = error: a deprecation in this code or in a
dependency fails the run instead of scrolling past in the summary. Both CI
test jobs start the interpreter with -X warn_default_encoding, so a text read
without an explicit encoding fails too. Set on the interpreter rather than in
the job's env so Poetry's own process stays quiet. The suite was already
clean under both flags with dependencies included (140 passed); a sanity
test with a stray DeprecationWarning fails as intended.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The 13 re-raises the bugbear audit (B904) found now say what they are.
`from e` where the new error translates the caught one: the integer and
log-format parsers and the algorithm lookup in config.py, the rate-limit
syntax check in limiter.py, the TOML read in scripts/release.py, the profile
URL validator in schemas/user.py, IntegrityError to IdentifierTakenError in
services/users.py, and PyJWT errors to the 401 in auth.py. `from None` for
the four generic 500s in routers/users.py, whose cause is already logged
with logger.exception and must not reach the client. No response changes;
tracebacks now distinguish "caused by" from "raised while handling".
AGENTS.md records the rule.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@JsNcAr
JsNcAr merged commit f8b2318 into main Oct 15, 2026
2 checks passed
@JsNcAr
JsNcAr deleted the refactor/exception-chaining branch October 15, 2026 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant