mirror of
https://github.com/goauthentik/authentik
synced 2026-05-09 08:32:47 +02:00
lib: match exception_to_dict locals behaviour (#17006) Signed-off-by: Jens Langhammer <jens@goauthentik.io> Co-authored-by: Jens L. <jens@goauthentik.io>
29 lines
890 B
Python
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__))
|