Spaces:
Runtime error

todoapp_chatbot / utils /errors.py
Maheen Ghouri
something add
dae0dd9
Raw History Blame Contribute Delete
1.87 kB
from fastapi import HTTPException, status
from typing import Optional
class BaseError(HTTPException):
"""Base error class with consistent response format"""
def __init__(self, status_code: int, detail: str):
super().__init__(
status_code=status_code,
detail=detail
)
class TaskNotFoundError(BaseError):
def __init__(self, task_id: str):
super().__init__(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Task with id {task_id} not found"
)
class UnauthorizedError(BaseError):
def __init__(self):
super().__init__(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not authorized to perform this action"
)
class UserNotFoundError(BaseError):
def __init__(self):
super().__init__(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
class DuplicateEmailError(BaseError):
def __init__(self):
super().__init__(
status_code=status.HTTP_409_CONFLICT,
detail="Email already registered"
)
class InvalidTokenError(BaseError):
def __init__(self):
super().__init__(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid or expired token"
)
class ValidationError(BaseError):
def __init__(self, detail: str):
super().__init__(
status_code=status.HTTP_400_BAD_REQUEST,
detail=detail
)
class LoginError(BaseError):
def __init__(self):
super().__init__(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect email or password"
)
def create_error_response(detail: str, status_code: int):
"""Create consistent error response format"""
return {"detail": detail, "status": status_code}