from praisonai_platform.exceptions import ( PlatformError, NotFoundError, DuplicateError, AuthenticationError, AuthorizationError, ValidationError)
2
Raise Domain-Specific Errors
# In a servicedef get_workspace(workspace_id: str): if not workspace_exists(workspace_id): raise NotFoundError(f"Workspace {workspace_id} not found") return workspacedef create_project(project_data): if project_exists(project_data.name): raise DuplicateError(f"Project {project_data.name} already exists") return create_new_project(project_data)
The PlatformError base class provides common functionality:
from praisonai_platform.exceptions import PlatformError# All platform exceptions inherit from thistry: # Platform operations passexcept PlatformError as e: # Catches any platform-specific error logger.error(f"Platform error: {e}")
from praisonai_platform.exceptions import NotFoundError, ValidationErrorasync def get_workspace(workspace_id: str): """Get workspace by ID.""" if not workspace_id: raise ValidationError("Workspace ID is required") workspace = await workspace_service.get(workspace_id) if not workspace: raise NotFoundError(f"Workspace {workspace_id} not found") return workspace@app.get("/workspaces/{workspace_id}")async def get_workspace_endpoint(workspace_id: str): # Exceptions automatically handled by FastAPI handlers return await get_workspace(workspace_id)
# Good: Specific and actionableraise NotFoundError(f"Workspace {workspace_id} not found")raise ValidationError("Project name must be 3-50 characters")# Avoid: Generic messagesraise NotFoundError("Not found")raise ValidationError("Invalid input")
Chain exceptions to preserve original error context:
try: # Database operation result = db.create_project(project_data)except DatabaseError as e: raise DuplicateError(f"Project {project_data.name} already exists") from e
Take advantage of the exception hierarchy. Catch PlatformError to handle all platform exceptions, or catch specific types for targeted handling.
try: workspace_service.get(workspace_id)except NotFoundError: # Handle specific case return create_default_workspace()except PlatformError as e: # Handle any other platform error logger.error(f"Platform error: {e}") raise