Skip to content

fix(logger): expose the original traceback on unhandled errors - #18

Merged
SrStamm merged 1 commit into
mainfrom
fix/error-visibility
Oct 17, 2026
Merged

SrStamm merged 1 commit into
mainfrom
fix/error-visibility

Conversation

@SrStamm

@SrStamm SrStamm commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Summary

  • Unhandled errors never reached the logs as such: dict_tracebacks renders exception as a list of dicts while the dev ConsoleRenderer expects a string, so every 5xx was recorded as TypeError: can only concatenate str (not "list") to str and the real error was lost.
  • The dev renderer also defaults to RichTracebackFormatter, and rich's Traceback.extract has no cycle guard for exception chains. anyio plus BaseHTTPMiddleware build a cyclic graph (RuntimeError.__context__ is an ExceptionGroup whose exceptions holds the same RuntimeError), so logging raised RecursionError, the handler died and the 500 came back with an empty body.
  • Dev now renders exceptions with the stdlib plain_traceback formatter (stdlib guards cycles; rich does not), dict_tracebacks stays only in the JSON chain, and JSONRenderer is instantiated instead of passed as a class.

Changes

File Change
core/logger.py Split dev/prod processor chains; dict_tracebacks only with the JSON renderer; JSONRenderer() instantiated; dev exception formatter set to plain_traceback; handler passes exc_info=exc
main.py Middleware passes the exception object as exc_info instead of a pre-rendered string
test/test_routers/test_logger.py New regression tests: a 500 must return JSON, and the original traceback must reach the logs

Test plan

  • Full suite: pytest -q --disable-warnings → 157 passed, 20 warnings
  • CI scope: pytest -q --disable-warnings ./test/test_routers → 70 passed
  • Regression test proven against the pre-fix code: it fails (Traceback never reaches the logs, the handler logs the masking TypeError) and passes with the fix
  • End-to-end behaviour: a raising endpoint returns 500 {"detail": "Internal Server Error"} and logs the original traceback. Pre-fix it logged error='can only concatenate str (not "list") to str' instead of the real error
  • Pending B4/B6/B7 changes (README demo password, db/database.py, CORS origin) kept out of this PR

Trade-off

Colored rich tracebacks in dev are dropped in favour of the stdlib formatter, which walks exception chains with cycle detection. Keeping rich would leave the error path able to kill itself on the first cyclic exception, which is exactly the bug being fixed. A safety-net wrapper around rich logging is possible later if the richer output is wanted.

Unhandled errors never reached the logs as such. structlog's
`dict_tracebacks` renders `exception` as a list of dicts while the dev
`ConsoleRenderer` expects a string, so every 5xx was recorded as
`TypeError: can only concatenate str (not "list") to str` and the real
error was lost.

The dev renderer also defaults to `RichTracebackFormatter`, and rich's
`Traceback.extract` has no cycle guard for exception chains. anyio plus
`BaseHTTPMiddleware` build a cyclic graph (`RuntimeError.__context__` is
an `ExceptionGroup` whose `exceptions` holds the same `RuntimeError`),
so logging raised `RecursionError`, the handler died and the 500 came
back with an empty body.

- keep `dict_tracebacks` only in the JSON chain, it is exclusive to it
- instantiate `JSONRenderer()`, passing the class broke production
- use the stdlib `plain_traceback` formatter in dev, it does guard cycles
- pass the exception object as `exc_info` instead of a rendered string

test/test_logger.py locks both behaviours: a 500 must return JSON, and
the original traceback must reach the logs.
@SrStamm
SrStamm merged commit 0ea20de into main Oct 17, 2026
1 check passed
@SrStamm
SrStamm deleted the fix/error-visibility branch October 17, 2026 13:35
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