Files
authentik/authentik/lib/utils/errors.py
authentik-automation[bot] dfdd5ebc8c lib: match exception_to_dict locals behaviour (cherry-pick #17006) (#17016)
lib: match exception_to_dict locals behaviour (#17006)

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
Co-authored-by: Jens L. <jens@goauthentik.io>
2025-09-25 17:53:36 +02:00

29 lines
890 B
Python

"""error utils"""
from traceback import extract_tb
from structlog.tracebacks import ExceptionDictTransformer
from authentik.lib.config import CONFIG
from authentik.lib.utils.reflection import class_to_path
TRACEBACK_HEADER = "Traceback (most recent call last):"
_exception_transformer = ExceptionDictTransformer(show_locals=CONFIG.get_bool("debug"))
def exception_to_string(exc: Exception) -> str:
"""Convert exception to string stackrace"""
# Either use passed original exception or whatever we have
return "\n".join(
[
TRACEBACK_HEADER,
*[x.rstrip() for x in extract_tb(exc.__traceback__).format()],
f"{class_to_path(exc.__class__)}: {str(exc)}",
]
)
def exception_to_dict(exc: Exception) -> dict:
"""Format exception as a dictionary"""
return _exception_transformer((type(exc), exc, exc.__traceback__))