Skip to content

Commit

Permalink
Refactor to generic 500 error on unhandled exception
Browse files Browse the repository at this point in the history
Signed-off-by: jamshale <[email protected]>
  • Loading branch information
jamshale committed Sep 19, 2023
1 parent 2bce5a2 commit 7b18e03
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
8 changes: 0 additions & 8 deletions oidc-controller/api/core/logger_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,3 @@ def wrapper(*args, **kwargs):
return ret_val

return wrapper


def extract_detail_from_exception(exception_only_list) -> str:
try:
return exception_only_list[0].split(": ")[1:][0].rstrip()
except Exception:
logger.error(f"Failed to get exception details from: {exception_only_list}")
return "Unknown error"
13 changes: 5 additions & 8 deletions oidc-controller/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from fastapi import FastAPI
from starlette.requests import Request
from starlette.responses import Response
from fastapi.responses import JSONResponse
from fastapi.exceptions import HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi import status as http_status

Expand All @@ -22,7 +22,6 @@
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_detail_from_exception
from api.core.models import GenericErrorMessage
from api.core.oidc.provider import init_provider

Expand Down Expand Up @@ -99,17 +98,15 @@ async def logging_middleware(request: Request, call_next) -> Response:
logger.info("processed a request", status_code=response.status_code, process_time=process_time)
# Otherwise, extract the exception from traceback, log and return a 500 response
else:
logger.info("failed to process a request", status_code=500, process_time=process_time)
logger.info("failed to process a request", status_code=http_status.HTTP_500_INTERNAL_SERVER_ERROR, process_time=process_time)

# Need to explicitly log the traceback as json here. Not clear as to why.
if os.environ.get("LOG_WITH_JSON") is True:
if os.environ.get("LOG_WITH_JSON", True) is True:
logger.error(traceback.format_exc())

exc_type, exc_value, _ = sys.exc_info()
return JSONResponse(
raise HTTPException(
status_code=http_status.HTTP_500_INTERNAL_SERVER_ERROR,
content=GenericErrorMessage(
detail=extract_detail_from_exception(traceback.format_exception_only(exc_type, exc_value))).dict()
detail="Internal Server Error",
)


Expand Down

0 comments on commit 7b18e03

Please sign in to comment.