""" LandveX Admin Backend — Custom Exceptions """ from fastapi import HTTPException, status from typing import Optional, Dict, Any class LandvexException(Exception): """Base exception for LandveX application.""" def __init__( self, message: str, status_code: int = 500, details: Optional[Dict[str, Any]] = None ): self.message = message self.status_code = status_code self.details = details or {} super().__init__(self.message) class TenantNotFoundException(LandvexException): """Raised when a tenant is not found.""" def __init__(self, tenant_id: str): super().__init__( message=f"Tenant not found: {tenant_id}", status_code=status.HTTP_404_NOT_FOUND, details={"tenant_id": tenant_id} ) class TenantAlreadyExistsException(LandvexException): """Raised when attempting to create a tenant that already exists.""" def __init__(self, identifier: str, field: str = "slug"): super().__init__( message=f"Tenant with {field} '{identifier}' already exists", status_code=status.HTTP_409_CONFLICT, details={"field": field, "value": identifier} ) class TenantValidationException(LandvexException): """Raised when tenant data validation fails.""" def __init__(self, message: str, field: Optional[str] = None): details = {} if field: details["field"] = field super().__init__( message=message, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, details=details ) class TenantSchemaException(LandvexException): """Raised when tenant schema operations fail.""" def __init__(self, tenant_id: str, operation: str, original_error: str): super().__init__( message=f"Schema operation '{operation}' failed for tenant {tenant_id}: {original_error}", status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, details={"tenant_id": tenant_id, "operation": operation} ) class UnauthorizedTenantAccessException(LandvexException): """Raised when a user tries to access a tenant they don't have permission for.""" def __init__(self, tenant_id: str, user_id: str): super().__init__( message=f"User {user_id} is not authorized to access tenant {tenant_id}", status_code=status.HTTP_403_FORBIDDEN, details={"tenant_id": tenant_id, "user_id": user_id} ) class AuditLogException(LandvexException): """Raised when audit logging fails.""" def __init__(self, operation: str, original_error: str): super().__init__( message=f"Audit logging failed for operation '{operation}': {original_error}", status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, details={"operation": operation} ) def http_exception_handler(exc: LandvexException) -> HTTPException: """Convert LandvexException to FastAPI HTTPException.""" return HTTPException( status_code=exc.status_code, detail={ "error": exc.message, "details": exc.details } )