diff --git a/oidc-controller/api/core/logger_util.py b/oidc-controller/api/core/logger_util.py index 27bb772f..66e0bf61 100644 --- a/oidc-controller/api/core/logger_util.py +++ b/oidc-controller/api/core/logger_util.py @@ -23,9 +23,9 @@ def wrapper(*args, **kwargs): return wrapper -def extract_error_msg_from_traceback_exc(format_exc) -> str: +def extract_detail_from_exception(exception_only_list) -> str: try: - return format_exc.splitlines()[-1].split(": ")[1:][0] + return exception_only_list[0].split(": ")[1:][0].rstrip() except Exception: - logger.error(f"Failed to extract error message from traceback: {format_exc}") + logger.error(f"Failed to get exception details from: {exception_only_list}") return "Unknown error" \ No newline at end of file diff --git a/oidc-controller/api/main.py b/oidc-controller/api/main.py index bbbac2f8..dc5e3c3c 100644 --- a/oidc-controller/api/main.py +++ b/oidc-controller/api/main.py @@ -1,3 +1,4 @@ +import sys import traceback import structlog import os @@ -21,7 +22,7 @@ from .clientConfigurations.router import router as client_config_router from .db.session import init_db, get_db from .routers.socketio import sio_app -from api.core.logger_util import extract_error_msg_from_traceback_exc +from api.core.logger_util import extract_detail_from_exception from api.core.models import GenericErrorMessage from api.core.oidc.provider import init_provider @@ -104,10 +105,11 @@ async def logging_middleware(request: Request, call_next) -> Response: if os.environ.get("LOG_WITH_JSON") is True: logger.error(traceback.format_exc()) + exc_type, exc_value, _ = sys.exc_info() return JSONResponse( status_code=http_status.HTTP_500_INTERNAL_SERVER_ERROR, content=GenericErrorMessage( - detail=extract_error_msg_from_traceback_exc(traceback.format_exc())).dict() + detail=extract_detail_from_exception(traceback.format_exception_only(exc_type, exc_value))).dict() )