
- Set up project structure and dependencies - Create database models for users, posts, comments, and tags - Set up Alembic for database migrations - Implement user authentication (register, login) - Create CRUD endpoints for blog posts, comments, and tags - Add health check endpoint - Set up proper error handling - Update README with project details and setup instructions
55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
from fastapi import HTTPException, status
|
|
|
|
|
|
class BlogAPIError(HTTPException):
|
|
"""Base class for all API errors."""
|
|
|
|
def __init__(
|
|
self,
|
|
status_code: int,
|
|
detail: str,
|
|
headers: dict = None,
|
|
):
|
|
super().__init__(status_code=status_code, detail=detail, headers=headers)
|
|
|
|
|
|
class NotFoundError(BlogAPIError):
|
|
"""Raised when a resource is not found."""
|
|
|
|
def __init__(self, detail: str = "Resource not found"):
|
|
super().__init__(status_code=status.HTTP_404_NOT_FOUND, detail=detail)
|
|
|
|
|
|
class ForbiddenError(BlogAPIError):
|
|
"""Raised when a user is not allowed to access a resource."""
|
|
|
|
def __init__(self, detail: str = "You don't have permission to access this resource"):
|
|
super().__init__(status_code=status.HTTP_403_FORBIDDEN, detail=detail)
|
|
|
|
|
|
class UnauthorizedError(BlogAPIError):
|
|
"""Raised when a user is not authenticated."""
|
|
|
|
def __init__(self, detail: str = "Not authenticated"):
|
|
super().__init__(status_code=status.HTTP_401_UNAUTHORIZED, detail=detail)
|
|
|
|
|
|
class BadRequestError(BlogAPIError):
|
|
"""Raised when a request is malformed or invalid."""
|
|
|
|
def __init__(self, detail: str = "Bad request"):
|
|
super().__init__(status_code=status.HTTP_400_BAD_REQUEST, detail=detail)
|
|
|
|
|
|
class ConflictError(BlogAPIError):
|
|
"""Raised when a resource already exists."""
|
|
|
|
def __init__(self, detail: str = "Resource already exists"):
|
|
super().__init__(status_code=status.HTTP_409_CONFLICT, detail=detail)
|
|
|
|
|
|
class ValidationError(BlogAPIError):
|
|
"""Raised when validation fails."""
|
|
|
|
def __init__(self, detail: str = "Validation error"):
|
|
super().__init__(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=detail) |