- infra (k8s, kind, helm, docker) backbone is implemented - security: implementation + unit tests are done
60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
"""Custom HTTP exceptions for the API."""
|
|
|
|
from fastapi import HTTPException, status
|
|
|
|
|
|
class NotFoundError(HTTPException):
|
|
"""Resource not found."""
|
|
|
|
def __init__(self, detail: str = "Resource not found") -> None:
|
|
super().__init__(status_code=status.HTTP_404_NOT_FOUND, detail=detail)
|
|
|
|
|
|
class ConflictError(HTTPException):
|
|
"""Conflict with current state (e.g., version mismatch)."""
|
|
|
|
def __init__(self, detail: str = "Conflict with current state") -> None:
|
|
super().__init__(status_code=status.HTTP_409_CONFLICT, detail=detail)
|
|
|
|
|
|
class UnauthorizedError(HTTPException):
|
|
"""Authentication required or failed."""
|
|
|
|
def __init__(self, detail: str = "Not authenticated") -> None:
|
|
super().__init__(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail=detail,
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
|
|
class ForbiddenError(HTTPException):
|
|
"""Insufficient permissions."""
|
|
|
|
def __init__(self, detail: str = "Insufficient permissions") -> None:
|
|
super().__init__(status_code=status.HTTP_403_FORBIDDEN, detail=detail)
|
|
|
|
|
|
class BadRequestError(HTTPException):
|
|
"""Invalid request data."""
|
|
|
|
def __init__(self, detail: str = "Invalid request") -> None:
|
|
super().__init__(status_code=status.HTTP_400_BAD_REQUEST, detail=detail)
|
|
|
|
|
|
class ValidationError(HTTPException):
|
|
"""Validation failed."""
|
|
|
|
def __init__(self, detail: str = "Validation failed") -> None:
|
|
super().__init__(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=detail)
|
|
|
|
|
|
__all__ = [
|
|
"BadRequestError",
|
|
"ConflictError",
|
|
"ForbiddenError",
|
|
"NotFoundError",
|
|
"UnauthorizedError",
|
|
"ValidationError",
|
|
]
|