Project / core /utils /error_handling.py
puzan789's picture
updated
ad87194
raw
history blame
1.17 kB
from typing import Any, Dict, List
from fastapi import HTTPException
from core.models.response_handling_models import ErrorResponse, SuccessResponse, SuccessResponseUsermanagement
def create_success_response(code: int, data: Dict[str, Any]) -> SuccessResponse:
return SuccessResponse(
status="success",
code=code,
data=data
)
def create_error_response(code: int, message: str, details: List[Dict[str, Any]] = None) -> ErrorResponse:
if details is None:
details = []
return ErrorResponse(
status="error",
error={
"message": message,
"details": details
}
)
def raise_http_exception(code: int, message: str, details: List[Dict[str, Any]] = None) -> None:
error_response = create_error_response(code, message, details)
raise HTTPException(status_code=code, detail=error_response.json())
def success_response_user_management(code: int, message: str,
data: Dict[str, Any] = None):
return SuccessResponseUsermanagement(
status="success",
code=code,
message=message,
data=data or {}
)