Automated Action e1b1b89511 Create FastAPI REST API with SQLite
Features:
- Project structure with FastAPI framework
- SQLAlchemy models with SQLite database
- Alembic migrations system
- CRUD operations for items
- API routers with endpoints for items
- Health endpoint for monitoring
- Error handling and validation
- Comprehensive documentation
2025-05-18 05:45:33 +00:00

43 lines
1.1 KiB
Python

from fastapi import HTTPException, Request, status
from fastapi.responses import JSONResponse
class BaseAPIException(HTTPException):
def __init__(
self,
status_code: int,
detail: str = None,
headers: dict = None,
):
super().__init__(status_code=status_code, detail=detail, headers=headers)
class ItemNotFoundException(BaseAPIException):
def __init__(self, item_id: int):
super().__init__(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Item with ID {item_id} not found",
)
class ValidationError(BaseAPIException):
def __init__(self, detail: str = "Validation error"):
super().__init__(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=detail,
)
async def validation_exception_handler(request: Request, exc: ValidationError):
return JSONResponse(
status_code=exc.status_code,
content={"detail": exc.detail},
)
async def item_not_found_handler(request: Request, exc: ItemNotFoundException):
return JSONResponse(
status_code=exc.status_code,
content={"detail": exc.detail},
)