Spaces:
Runtime error
Runtime error
File size: 1,866 Bytes
f82e38a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 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} |