new api service and logic implemented
This commit is contained in:
188
Services/MongoDb/Models/exception_handlers.py
Normal file
188
Services/MongoDb/Models/exception_handlers.py
Normal file
@@ -0,0 +1,188 @@
|
||||
"""
|
||||
Exception handlers for MongoDB operations.
|
||||
|
||||
This module provides exception handlers for MongoDB-related errors,
|
||||
converting them to appropriate HTTP responses.
|
||||
"""
|
||||
|
||||
from typing import Callable, Any
|
||||
from fastapi import Request, status
|
||||
from fastapi.responses import JSONResponse
|
||||
from pymongo.errors import PyMongoError, DuplicateKeyError, ConnectionFailure
|
||||
|
||||
from ApiLibrary.common.line_number import get_line_number_for_error
|
||||
from Services.MongoDb.Models.exceptions import (
|
||||
MongoBaseException,
|
||||
MongoConnectionError,
|
||||
MongoDocumentNotFoundError,
|
||||
MongoValidationError,
|
||||
MongoDuplicateKeyError,
|
||||
PasswordHistoryError,
|
||||
PasswordReuseError,
|
||||
PasswordHistoryLimitError,
|
||||
InvalidPasswordDetailError,
|
||||
)
|
||||
from ErrorHandlers.ErrorHandlers.api_exc_handler import HTTPExceptionApi
|
||||
|
||||
|
||||
def handle_mongo_errors(func: Callable) -> Callable:
|
||||
"""Decorator to handle MongoDB operation errors.
|
||||
|
||||
Args:
|
||||
func: Function to wrap with error handling
|
||||
|
||||
Returns:
|
||||
Wrapped function with error handling
|
||||
"""
|
||||
|
||||
async def wrapper(*args, **kwargs) -> Any:
|
||||
try:
|
||||
return await func(*args, **kwargs)
|
||||
except ConnectionFailure as e:
|
||||
raise MongoConnectionError(
|
||||
message=str(e), details={"error_type": "connection_failure"}
|
||||
).to_http_exception()
|
||||
except DuplicateKeyError as e:
|
||||
raise MongoDuplicateKeyError(
|
||||
collection=e.details.get("namespace", "unknown"),
|
||||
key_pattern=e.details.get("keyPattern", {}),
|
||||
).to_http_exception()
|
||||
except PyMongoError as e:
|
||||
raise MongoBaseException(
|
||||
message=str(e), details={"error_type": "pymongo_error"}
|
||||
).to_http_exception()
|
||||
except Exception as e:
|
||||
raise HTTPExceptionApi(
|
||||
lang="en",
|
||||
error_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
loc=get_line_number_for_error(),
|
||||
sys_msg=str(e),
|
||||
)
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
async def mongo_base_exception_handler(
|
||||
request: Request, exc: MongoBaseException
|
||||
) -> JSONResponse:
|
||||
"""Handle base MongoDB exceptions.
|
||||
|
||||
Args:
|
||||
request: FastAPI request
|
||||
exc: MongoDB base exception
|
||||
|
||||
Returns:
|
||||
JSON response with error details
|
||||
"""
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code, content={"error": exc.to_http_exception()}
|
||||
)
|
||||
|
||||
|
||||
async def mongo_connection_error_handler(
|
||||
request: Request, exc: MongoConnectionError
|
||||
) -> JSONResponse:
|
||||
"""Handle MongoDB connection errors.
|
||||
|
||||
Args:
|
||||
request: FastAPI request
|
||||
exc: MongoDB connection error
|
||||
|
||||
Returns:
|
||||
JSON response with connection error details
|
||||
"""
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
content={"error": exc.to_http_exception()},
|
||||
)
|
||||
|
||||
|
||||
async def mongo_document_not_found_handler(
|
||||
request: Request, exc: MongoDocumentNotFoundError
|
||||
) -> JSONResponse:
|
||||
"""Handle document not found errors.
|
||||
|
||||
Args:
|
||||
request: FastAPI request
|
||||
exc: Document not found error
|
||||
|
||||
Returns:
|
||||
JSON response with not found error details
|
||||
"""
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
content={"error": exc.to_http_exception()},
|
||||
)
|
||||
|
||||
|
||||
async def mongo_validation_error_handler(
|
||||
request: Request, exc: MongoValidationError
|
||||
) -> JSONResponse:
|
||||
"""Handle validation errors.
|
||||
|
||||
Args:
|
||||
request: FastAPI request
|
||||
exc: Validation error
|
||||
|
||||
Returns:
|
||||
JSON response with validation error details
|
||||
"""
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
content={"error": exc.to_http_exception()},
|
||||
)
|
||||
|
||||
|
||||
async def mongo_duplicate_key_error_handler(
|
||||
request: Request, exc: MongoDuplicateKeyError
|
||||
) -> JSONResponse:
|
||||
"""Handle duplicate key errors.
|
||||
|
||||
Args:
|
||||
request: FastAPI request
|
||||
exc: Duplicate key error
|
||||
|
||||
Returns:
|
||||
JSON response with duplicate key error details
|
||||
"""
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_409_CONFLICT, content={"error": exc.to_http_exception()}
|
||||
)
|
||||
|
||||
|
||||
async def password_history_error_handler(
|
||||
request: Request, exc: PasswordHistoryError
|
||||
) -> JSONResponse:
|
||||
"""Handle password history errors.
|
||||
|
||||
Args:
|
||||
request: FastAPI request
|
||||
exc: Password history error
|
||||
|
||||
Returns:
|
||||
JSON response with password history error details
|
||||
"""
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code, content={"error": exc.to_http_exception()}
|
||||
)
|
||||
|
||||
|
||||
def register_exception_handlers(app: Any) -> None:
|
||||
"""Register all MongoDB exception handlers with FastAPI app.
|
||||
|
||||
Args:
|
||||
app: FastAPI application instance
|
||||
"""
|
||||
app.add_exception_handler(MongoBaseException, mongo_base_exception_handler)
|
||||
app.add_exception_handler(MongoConnectionError, mongo_connection_error_handler)
|
||||
app.add_exception_handler(
|
||||
MongoDocumentNotFoundError, mongo_document_not_found_handler
|
||||
)
|
||||
app.add_exception_handler(MongoValidationError, mongo_validation_error_handler)
|
||||
app.add_exception_handler(MongoDuplicateKeyError, mongo_duplicate_key_error_handler)
|
||||
app.add_exception_handler(PasswordHistoryError, password_history_error_handler)
|
||||
app.add_exception_handler(PasswordReuseError, password_history_error_handler)
|
||||
app.add_exception_handler(PasswordHistoryLimitError, password_history_error_handler)
|
||||
app.add_exception_handler(
|
||||
InvalidPasswordDetailError, password_history_error_handler
|
||||
)
|
||||
Reference in New Issue
Block a user