refactor: improve database middleware error handling
This commit is contained in:
parent
21180ea017
commit
9d2ea5a838
@ -1,4 +1,4 @@
|
|||||||
from fastapi import Request, status
|
from fastapi import Request, status, HTTPException
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
from webapi.middleware.freeleaps_auth_middleware import request_context_var
|
from webapi.middleware.freeleaps_auth_middleware import request_context_var
|
||||||
from common.log.module_logger import ModuleLogger
|
from common.log.module_logger import ModuleLogger
|
||||||
@ -58,14 +58,13 @@ class DatabaseMiddleware:
|
|||||||
request.state.db = tenant_db
|
request.state.db = tenant_db
|
||||||
request.state.product_id = product_id
|
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}")
|
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:
|
except HTTPException as e:
|
||||||
# Handle tenant not found or inactive (ValueError from TenantDBCache)
|
# Handle tenant not found or inactive (HTTPException from TenantDBCache)
|
||||||
await self.module_logger.log_error(f"Tenant error for {product_id}: {str(e)}")
|
await self.module_logger.log_error(f"Tenant error for {product_id}: [{e.status_code}] {e.detail}")
|
||||||
response = JSONResponse(
|
response = JSONResponse(
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
status_code=e.status_code,
|
||||||
content={"detail": str(e)}
|
content={"detail": e.detail}
|
||||||
)
|
)
|
||||||
return await response(scope, receive, send)
|
return await response(scope, receive, send)
|
||||||
|
|
||||||
@ -76,3 +75,5 @@ class DatabaseMiddleware:
|
|||||||
content={"detail": "Database connection 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 fastapi.responses import JSONResponse
|
||||||
from webapi.middleware.freeleaps_auth_middleware import request_context_var
|
from webapi.middleware.freeleaps_auth_middleware import request_context_var
|
||||||
from common.log.module_logger import ModuleLogger
|
from common.log.module_logger import ModuleLogger
|
||||||
@ -58,14 +58,13 @@ class DatabaseMiddleware:
|
|||||||
request.state.db = tenant_db
|
request.state.db = tenant_db
|
||||||
request.state.product_id = product_id
|
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}")
|
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:
|
except HTTPException as e:
|
||||||
# Handle tenant not found or inactive (ValueError from TenantDBCache)
|
# Handle tenant not found or inactive (HTTPException from TenantDBCache)
|
||||||
await self.module_logger.log_error(f"Tenant error for {product_id}: {str(e)}")
|
await self.module_logger.log_error(f"Tenant error for {product_id}: [{e.status_code}] {e.detail}")
|
||||||
response = JSONResponse(
|
response = JSONResponse(
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
status_code=e.status_code,
|
||||||
content={"detail": str(e)}
|
content={"detail": e.detail}
|
||||||
)
|
)
|
||||||
return await response(scope, receive, send)
|
return await response(scope, receive, send)
|
||||||
|
|
||||||
@ -76,3 +75,5 @@ class DatabaseMiddleware:
|
|||||||
content={"detail": "Database connection 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