Files
daily-journal-prompt/backend/app/core/exceptions.py
2026-01-03 11:18:56 -07:00

173 lines
4.5 KiB
Python

"""
Custom exceptions for the application.
"""
from typing import Any, Dict, Optional
from fastapi import HTTPException, status
class DailyJournalPromptException(HTTPException):
"""Base exception for Daily Journal Prompt application."""
def __init__(
self,
status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR,
detail: Any = None,
headers: Optional[Dict[str, str]] = None,
) -> None:
super().__init__(status_code=status_code, detail=detail, headers=headers)
class ValidationError(DailyJournalPromptException):
"""Exception for validation errors."""
def __init__(
self,
detail: Any = "Validation error",
headers: Optional[Dict[str, str]] = None,
) -> None:
super().__init__(
status_code=status.HTTP_400_BAD_REQUEST,
detail=detail,
headers=headers,
)
class NotFoundError(DailyJournalPromptException):
"""Exception for resource not found errors."""
def __init__(
self,
detail: Any = "Resource not found",
headers: Optional[Dict[str, str]] = None,
) -> None:
super().__init__(
status_code=status.HTTP_404_NOT_FOUND,
detail=detail,
headers=headers,
)
class UnauthorizedError(DailyJournalPromptException):
"""Exception for unauthorized access errors."""
def __init__(
self,
detail: Any = "Unauthorized access",
headers: Optional[Dict[str, str]] = None,
) -> None:
super().__init__(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=detail,
headers=headers,
)
class ForbiddenError(DailyJournalPromptException):
"""Exception for forbidden access errors."""
def __init__(
self,
detail: Any = "Forbidden access",
headers: Optional[Dict[str, str]] = None,
) -> None:
super().__init__(
status_code=status.HTTP_403_FORBIDDEN,
detail=detail,
headers=headers,
)
class AIServiceError(DailyJournalPromptException):
"""Exception for AI service errors."""
def __init__(
self,
detail: Any = "AI service error",
headers: Optional[Dict[str, str]] = None,
) -> None:
super().__init__(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=detail,
headers=headers,
)
class DataServiceError(DailyJournalPromptException):
"""Exception for data service errors."""
def __init__(
self,
detail: Any = "Data service error",
headers: Optional[Dict[str, str]] = None,
) -> None:
super().__init__(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=detail,
headers=headers,
)
class ConfigurationError(DailyJournalPromptException):
"""Exception for configuration errors."""
def __init__(
self,
detail: Any = "Configuration error",
headers: Optional[Dict[str, str]] = None,
) -> None:
super().__init__(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=detail,
headers=headers,
)
class PromptPoolEmptyError(DailyJournalPromptException):
"""Exception for empty prompt pool."""
def __init__(
self,
detail: Any = "Prompt pool is empty",
headers: Optional[Dict[str, str]] = None,
) -> None:
super().__init__(
status_code=status.HTTP_400_BAD_REQUEST,
detail=detail,
headers=headers,
)
class InsufficientPoolSizeError(DailyJournalPromptException):
"""Exception for insufficient pool size."""
def __init__(
self,
current_size: int,
requested: int,
headers: Optional[Dict[str, str]] = None,
) -> None:
detail = f"Pool only has {current_size} prompts, requested {requested}"
super().__init__(
status_code=status.HTTP_400_BAD_REQUEST,
detail=detail,
headers=headers,
)
class TemplateNotFoundError(DailyJournalPromptException):
"""Exception for missing template files."""
def __init__(
self,
template_name: str,
headers: Optional[Dict[str, str]] = None,
) -> None:
detail = f"Template not found: {template_name}"
super().__init__(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=detail,
headers=headers,
)