diff --git a/apps/authentication/webapi/middleware/database_middleware.py b/apps/authentication/webapi/middleware/database_middleware.py index e9f12db..4e19362 100644 --- a/apps/authentication/webapi/middleware/database_middleware.py +++ b/apps/authentication/webapi/middleware/database_middleware.py @@ -1,4 +1,4 @@ -from fastapi import Request, status +from fastapi import Request, status, HTTPException from fastapi.responses import JSONResponse from webapi.middleware.freeleaps_auth_middleware import request_context_var from common.log.module_logger import ModuleLogger @@ -58,14 +58,13 @@ class DatabaseMiddleware: request.state.db = tenant_db request.state.product_id = product_id await self.module_logger.log_info(f"Successfully retrieved cached tenant database with Beanie for product_id: {product_id}") - return await self.app(scope, receive, send) - - except ValueError as e: - # Handle tenant not found or inactive (ValueError from TenantDBCache) - await self.module_logger.log_error(f"Tenant error for {product_id}: {str(e)}") + + except HTTPException as e: + # Handle tenant not found or inactive (HTTPException from TenantDBCache) + await self.module_logger.log_error(f"Tenant error for {product_id}: [{e.status_code}] {e.detail}") response = JSONResponse( - status_code=status.HTTP_404_NOT_FOUND, - content={"detail": str(e)} + status_code=e.status_code, + content={"detail": e.detail} ) return await response(scope, receive, send) @@ -75,4 +74,6 @@ class DatabaseMiddleware: status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={"detail": "Database connection error"} ) - return await response(scope, receive, send) \ No newline at end of file + return await response(scope, receive, send) + + return await self.app(scope, receive, send) \ No newline at end of file diff --git a/apps/notification/webapi/middleware/database_middleware.py b/apps/notification/webapi/middleware/database_middleware.py index e9f12db..7a3c71e 100644 --- a/apps/notification/webapi/middleware/database_middleware.py +++ b/apps/notification/webapi/middleware/database_middleware.py @@ -1,4 +1,4 @@ -from fastapi import Request, status +from fastapi import Request, status, HTTPException from fastapi.responses import JSONResponse from webapi.middleware.freeleaps_auth_middleware import request_context_var from common.log.module_logger import ModuleLogger @@ -49,7 +49,7 @@ class DatabaseMiddleware: request.state.product_id = None await self.module_logger.log_info(f"Successfully initialized main database with tenant models") return await self.app(scope, receive, send) - + try: # Get tenant-specific database with Beanie already initialized (cached) await self.module_logger.log_info(f"Attempting to get tenant database for product_id: {product_id}") @@ -58,14 +58,13 @@ class DatabaseMiddleware: request.state.db = tenant_db request.state.product_id = product_id await self.module_logger.log_info(f"Successfully retrieved cached tenant database with Beanie for product_id: {product_id}") - return await self.app(scope, receive, send) - - except ValueError as e: - # Handle tenant not found or inactive (ValueError from TenantDBCache) - await self.module_logger.log_error(f"Tenant error for {product_id}: {str(e)}") + + except HTTPException as e: + # Handle tenant not found or inactive (HTTPException from TenantDBCache) + await self.module_logger.log_error(f"Tenant error for {product_id}: [{e.status_code}] {e.detail}") response = JSONResponse( - status_code=status.HTTP_404_NOT_FOUND, - content={"detail": str(e)} + status_code=e.status_code, + content={"detail": e.detail} ) return await response(scope, receive, send) @@ -75,4 +74,6 @@ class DatabaseMiddleware: status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={"detail": "Database connection error"} ) - return await response(scope, receive, send) \ No newline at end of file + return await response(scope, receive, send) + + return await self.app(scope, receive, send) \ No newline at end of file