errors.py 650 B

12345678910111213141516171819202122232425
  1. """
  2. agentpaas.api.errors — Standardized error responses.
  3. """
  4. from pydantic import BaseModel
  5. from typing import Optional
  6. from fastapi import HTTPException
  7. import uuid
  8. class ErrorResponse(BaseModel):
  9. code: str
  10. message: str
  11. request_id: str = ""
  12. details: Optional[dict] = None
  13. def api_error(status: int, code: str, message: str, details: dict = None) -> HTTPException:
  14. """Standardized API error with request tracking."""
  15. body = ErrorResponse(
  16. code=code,
  17. message=message,
  18. request_id=str(uuid.uuid4())[:8],
  19. details=details,
  20. )
  21. raise HTTPException(status_code=status, detail=body.dict())