Skip to content

Commit

Permalink
handle missing exc_info in ExceptionDictTransformer (#657)
Browse files Browse the repository at this point in the history
* handle missing exc_info in ExceptionDictTransformer

- Added a new utility function is_missing_exc_info to check if exc_info is missing.
- Updated ExceptionDictTransformer to return an empty list when exc_info is missing.
- Modified _format_exception to use the new is_missing_exc_info function.
- Added a test case to verify that ExceptionDictTransformer returns an empty list when exc_info is missing.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update tests/test_tracebacks.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Hynek Schlawack <[email protected]>
  • Loading branch information
3 people authored Nov 9, 2024
1 parent 6aaa0e2 commit 6f9ed85
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/structlog/_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@
from .typing import ExcInfo


def is_missing_exc_info(exc_info: ExcInfo) -> bool:
"""
Return True if exc_info is the missing value.
"""
return exc_info == (None, None, None) # type: ignore[comparison-overlap]


def _format_exception(exc_info: ExcInfo) -> str:
"""
Prettyprint an `exc_info` tuple.
Shamelessly stolen from stdlib's logging module.
"""
if exc_info == (None, None, None): # type: ignore[comparison-overlap]
if is_missing_exc_info(exc_info):
return "MISSING"

sio = StringIO()
Expand Down
4 changes: 4 additions & 0 deletions src/structlog/tracebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from types import ModuleType, TracebackType
from typing import Any, Iterable, Sequence, Tuple, Union

from ._frames import is_missing_exc_info


try:
import rich
Expand Down Expand Up @@ -412,6 +414,8 @@ def __init__(
self.use_rich = use_rich

def __call__(self, exc_info: ExcInfo) -> list[dict[str, Any]]:
if is_missing_exc_info(exc_info):
return []
trace = extract(
*exc_info,
show_locals=self.show_locals,
Expand Down
11 changes: 11 additions & 0 deletions tests/test_tracebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,3 +730,14 @@ def test_json_traceback_value_error(
monkeypatch.setattr(kwargs["suppress"][0], "__file__", None)
with pytest.raises(ValueError, match=next(iter(kwargs.keys()))):
tracebacks.ExceptionDictTransformer(**kwargs)


def test_exception_dict_transformer_missing_exc_info():
"""
ExceptionDictTransformer returns an empty list if exc_info is missing.
"""
transformer = tracebacks.ExceptionDictTransformer()

result = transformer(exc_info=(None, None, None))

assert [] == result

0 comments on commit 6f9ed85

Please sign in to comment.