Skip to content

Commit

Permalink
Refactor exception detail extraction
Browse files Browse the repository at this point in the history
Signed-off-by: jamshale <jamiehalebc@gmail.com>
  • Loading branch information
jamshale committed Sep 19, 2023
1 parent 1ce231f commit 2bce5a2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 3 additions & 3 deletions oidc-controller/api/core/logger_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 4 additions & 2 deletions oidc-controller/api/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import traceback
import structlog
import os
Expand All @@ -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

Expand Down Expand Up @@ -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()
)


Expand Down

0 comments on commit 2bce5a2

Please sign in to comment.