Skip to content

tracebacks: Handle ExceptionGroup #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/

## [Unreleased](https://github.com/hynek/structlog/compare/25.2.0...HEAD)

- `structlog.tracebacks` handles exception groups.
`structlog.tracebacks.Stack` has two new fields, `is_group: bool` and `exceptions: list[Trace]`.
This works similarly to what Rich v14.0.0 does.
[#720](https://github.com/hynek/structlog/pull/720)


## [25.2.0](https://github.com/hynek/structlog/compare/25.1.0...25.2.0) - 2025-03-11

Expand Down
2 changes: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ API Reference
... 1 / 0
... except ZeroDivisionError:
... log.exception("Cannot compute!")
{"event": "Cannot compute!", "exception": [{"exc_type": "ZeroDivisionError", "exc_value": "division by zero", "exc_notes": [], "syntax_error": null, "is_cause": false, "frames": [{"filename": "<doctest default[3]>", "lineno": 2, "name": "<module>", "locals": {..., "var": "'spam'"}}]}]}
{"event": "Cannot compute!", "exception": [{"exc_type": "ZeroDivisionError", "exc_value": "division by zero", "exc_notes": [], "syntax_error": null, "is_cause": false, "frames": [{"filename": "<doctest default[3]>", "lineno": 2, "name": "<module>", "locals": {..., "var": "'spam'"}}], "is_group": false, "exceptions": []}]}

.. autoclass:: KeyValueRenderer

Expand Down
44 changes: 40 additions & 4 deletions src/structlog/tracebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import os
import os.path
import sys

from dataclasses import asdict, dataclass, field
from traceback import walk_tb
Expand Down Expand Up @@ -84,6 +85,8 @@ class Stack:

.. versionchanged:: 25.2.0
Added the *exc_notes* field.
.. versionchanged:: 25.3.0
Added the *is_group* and *exceptions* fields.
"""

exc_type: str
Expand All @@ -92,6 +95,8 @@ class Stack:
syntax_error: SyntaxError_ | None = None
is_cause: bool = False
frames: list[Frame] = field(default_factory=list)
is_group: bool = False
exceptions: list[Trace] = field(default_factory=list)


@dataclass
Expand Down Expand Up @@ -225,6 +230,8 @@ def extract(
.. versionchanged:: 24.3.0
Added *locals_max_length*, *locals_hide_sunder*, *locals_hide_dunder*
and *use_rich* arguments.
.. versionchanged:: 25.3.0
Handle exception groups.
"""

stacks: list[Stack] = []
Expand All @@ -240,6 +247,24 @@ def extract(
is_cause=is_cause,
)

if sys.version_info >= (3, 11):
if isinstance(exc_value, (BaseExceptionGroup, ExceptionGroup)): # noqa: F821
stack.is_group = True
for exception in exc_value.exceptions:
stack.exceptions.append(
extract(
type(exception),
exception,
exception.__traceback__,
show_locals=show_locals,
locals_max_length=locals_max_length,
locals_max_string=locals_max_string,
locals_hide_dunder=locals_hide_dunder,
locals_hide_sunder=locals_hide_sunder,
use_rich=use_rich,
)
)

if isinstance(exc_value, SyntaxError):
stack.syntax_error = SyntaxError_(
offset=exc_value.offset or 0,
Expand Down Expand Up @@ -377,6 +402,9 @@ class ExceptionDictTransformer:
.. versionchanged:: 25.1.0
*locals_max_length* and *locals_max_string* may be None to disable
truncation.

.. versionchanged:: 25.3.0
Handle exception groups.
"""

def __init__(
Expand Down Expand Up @@ -451,13 +479,21 @@ def __call__(self, exc_info: ExcInfo) -> list[dict[str, Any]]:
*stack.frames[-half:],
]

stacks = [asdict(stack) for stack in trace.stacks]
for stack_dict in stacks:
return self._as_dict(trace)

def _as_dict(self, trace: Trace) -> list[dict[str, Any]]:
stack_dicts = []
for stack in trace.stacks:
stack_dict = asdict(stack)
for frame_dict in stack_dict["frames"]:
if frame_dict["locals"] is None or any(
frame_dict["filename"].startswith(path)
for path in self.suppress
):
del frame_dict["locals"]

return stacks
if stack.is_group:
stack_dict["exceptions"] = [
self._as_dict(t) for t in stack.exceptions
]
stack_dicts.append(stack_dict)
return stack_dicts
155 changes: 155 additions & 0 deletions tests/test_tracebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from __future__ import annotations

import asyncio
import inspect
import json
import sys
Expand Down Expand Up @@ -567,6 +568,84 @@ def bar(n):
]


@pytest.mark.skipif(
sys.version_info < (3, 11), reason="Requires Python 3.11 or higher"
)
def test_exception_groups() -> None:
"""
Exception groups are detected and a list of Trace instances is added to
the exception group's Trace.
"""
lineno = get_next_lineno()

async def t1() -> None:
1 // 0

async def t2() -> None:
raise ValueError("Blam!")

async def main():
async with asyncio.TaskGroup() as tg:
tg.create_task(t1())
tg.create_task(t2())

try:
asyncio.run(main())
except Exception as e:
trace = tracebacks.extract(type(e), e, e.__traceback__)

assert "ExceptionGroup" == trace.stacks[0].exc_type
assert (
"unhandled errors in a TaskGroup (2 sub-exceptions)"
== trace.stacks[0].exc_value
)
exceptptions = trace.stacks[0].exceptions
assert [
tracebacks.Trace(
stacks=[
tracebacks.Stack(
exc_type="ZeroDivisionError",
exc_value="integer division or modulo by zero",
exc_notes=[],
syntax_error=None,
is_cause=False,
frames=[
tracebacks.Frame(
filename=__file__,
lineno=lineno + 2,
name="t1",
locals=None,
)
],
is_group=False,
exceptions=[],
)
]
),
tracebacks.Trace(
stacks=[
tracebacks.Stack(
exc_type="ValueError",
exc_value="Blam!",
exc_notes=[],
syntax_error=None,
is_cause=False,
frames=[
tracebacks.Frame(
filename=__file__,
lineno=lineno + 5,
name="t2",
locals=None,
)
],
is_group=False,
exceptions=[],
)
]
),
] == exceptptions


@pytest.mark.parametrize(
("kwargs", "local_variable"),
[
Expand Down Expand Up @@ -623,6 +702,7 @@ def test_json_traceback():
"exc_type": "ZeroDivisionError",
"exc_value": "division by zero",
"exc_notes": [],
"exceptions": [],
"frames": [
{
"filename": __file__,
Expand All @@ -631,6 +711,7 @@ def test_json_traceback():
}
],
"is_cause": False,
"is_group": False,
"syntax_error": None,
},
] == result
Expand All @@ -657,6 +738,7 @@ def test_json_traceback_with_notes():
"exc_type": "ZeroDivisionError",
"exc_value": "division by zero",
"exc_notes": ["This is a note.", "This is another note."],
"exceptions": [],
"frames": [
{
"filename": __file__,
Expand All @@ -665,6 +747,7 @@ def test_json_traceback_with_notes():
}
],
"is_cause": False,
"is_group": False,
"syntax_error": None,
},
] == result
Expand All @@ -687,6 +770,7 @@ def test_json_traceback_locals_max_string():
"exc_type": "ZeroDivisionError",
"exc_value": "division by zero",
"exc_notes": [],
"exceptions": [],
"frames": [
{
"filename": __file__,
Expand All @@ -700,6 +784,7 @@ def test_json_traceback_locals_max_string():
}
],
"is_cause": False,
"is_group": False,
"syntax_error": None,
},
] == result
Expand Down Expand Up @@ -844,6 +929,76 @@ def test_json_traceback_value_error(
tracebacks.ExceptionDictTransformer(**kwargs)


@pytest.mark.skipif(
sys.version_info < (3, 11), reason="Requires Python 3.11 or higher"
)
def test_json_exception_groups() -> None:
"""
When rendered as JSON, the "Trace.stacks" is stripped, so "exceptions" is
a list of lists and not a list of objects (wht a single "stacks" attribute.
"""

lineno = get_next_lineno()

async def t1() -> None:
1 // 0

async def t2() -> None:
raise ValueError("Blam!")

async def main():
async with asyncio.TaskGroup() as tg:
tg.create_task(t1())
tg.create_task(t2())

try:
asyncio.run(main())
except Exception as e:
format_json = tracebacks.ExceptionDictTransformer(show_locals=False)
result = format_json((type(e), e, e.__traceback__))

assert "ExceptionGroup" == result[0]["exc_type"]
exceptptions = result[0]["exceptions"]
assert [
[
{
"exc_type": "ZeroDivisionError",
"exc_value": "integer division or modulo by zero",
"exc_notes": [],
"syntax_error": None,
"is_cause": False,
"frames": [
{
"filename": __file__,
"lineno": lineno + 2,
"name": "t1",
}
],
"is_group": False,
"exceptions": [],
}
],
[
{
"exc_type": "ValueError",
"exc_value": "Blam!",
"exc_notes": [],
"syntax_error": None,
"is_cause": False,
"frames": [
{
"filename": __file__,
"lineno": lineno + 5,
"name": "t2",
}
],
"is_group": False,
"exceptions": [],
}
],
] == exceptptions


class TestLogException:
"""
Higher level integration tests for `Logger.exception()`.
Expand Down