Skip to content

Commit

Permalink
Add detailed logging and session checks in DBSessionMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ahdamin committed Sep 27, 2024
1 parent 62141a9 commit 648b94e
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions genotype_api/api/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,46 @@ def __init__(self, app):
super().__init__(app)

async def dispatch(self, request: Request, call_next):
session = None
message = "Internal server error: database session error."
error_message = JSONResponse(status_code=500, content={"message": message})
session = get_session()
if session is None:
LOG.error("No database session found.")
return JSONResponse(
status_code=500, content={"message": "Internal server error: No database session."}
)

try:
session = get_session()
if session is None:
LOG.debug(f"No database session found.")
return error_message
elif session.dirty:
response = await call_next(request)

if session.dirty:
session.flush()
response = await call_next(request)
return response
else:
response = await call_next(request)
return response

return response

except PendingRollbackError as e:
if session and session.is_active:
LOG.error("Pending rollback error, rolling back session", exc_info=True)
if session.is_active:
session.rollback()
LOG.debug(f"DB session error occurred: {e}")
return error_message
return JSONResponse(
status_code=500, content={"message": "Internal server error: Pending rollback."}
)

except OperationalError as e:
LOG.debug(f"Database connection lost: {e}")
return error_message
LOG.error("Operational error: database connection lost", exc_info=True)
if session.is_active:
session.rollback()
return JSONResponse(
status_code=500,
content={"message": "Internal server error: Database connection lost."},
)

except Exception as e:
LOG.debug(f"DB session occurred: {e}")
return error_message
LOG.error(f"Unexpected error occurred: {e}", exc_info=True)
if session.is_active:
session.rollback()
return JSONResponse(
status_code=500,
content={"message": "Internal server error: Unexpected error occurred."},
)

finally:
if session:
close_session()
close_session()

0 comments on commit 648b94e

Please sign in to comment.