Skip to content
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

handle missing exc_info in ExceptionDictTransformer #657

Merged
Show file tree
Hide file tree
Changes from 4 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
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