Merge pull request 'refactor: improve database middleware error handling' (#88) from tania_middleware into dev
Reviewed-on: freeleaps/freeleaps-service-hub#88
This commit is contained in:
commit
6536166be5
@ -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)
|
||||
return await response(scope, receive, send)
|
||||
|
||||
return await self.app(scope, receive, send)
|
||||
@ -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)
|
||||
return await response(scope, receive, send)
|
||||
|
||||
return await self.app(scope, receive, send)
|
||||
Loading…
Reference in New Issue
Block a user