Repository navigation
fix(logger): expose the original traceback on unhandled errors - #18
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
dict_tracebacksrendersexceptionas a list of dicts while the devConsoleRendererexpects a string, so every 5xx was recorded asTypeError: can only concatenate str (not "list") to strand the real error was lost.RichTracebackFormatter, and rich'sTraceback.extracthas no cycle guard for exception chains. anyio plusBaseHTTPMiddlewarebuild a cyclic graph (RuntimeError.__context__is anExceptionGroupwhoseexceptionsholds the sameRuntimeError), so logging raisedRecursionError, the handler died and the 500 came back with an empty body.plain_tracebackformatter (stdlib guards cycles; rich does not),dict_tracebacksstays only in the JSON chain, andJSONRendereris instantiated instead of passed as a class.Changes
core/logger.pydict_tracebacksonly with the JSON renderer;JSONRenderer()instantiated; dev exception formatter set toplain_traceback; handler passesexc_info=excmain.pyexc_infoinstead of a pre-rendered stringtest/test_routers/test_logger.pyTest plan
pytest -q --disable-warnings→ 157 passed, 20 warningspytest -q --disable-warnings ./test/test_routers→ 70 passedTracebacknever reaches the logs, the handler logs the maskingTypeError) and passes with the fix500 {"detail": "Internal Server Error"}and logs the original traceback. Pre-fix it loggederror='can only concatenate str (not "list") to str'instead of the real errordb/database.py, CORS origin) kept out of this PRTrade-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.