diff --git a/backend/app/core/exceptions.py b/backend/app/core/exceptions.py new file mode 100644 index 0000000..e643c6e --- /dev/null +++ b/backend/app/core/exceptions.py @@ -0,0 +1,52 @@ +"""Custom application exceptions.""" +from fastapi import HTTPException, status + + +class AppException(HTTPException): + """Base application exception.""" + + def __init__( + self, + detail: str, + status_code: int = status.HTTP_400_BAD_REQUEST, + ): + super().__init__(status_code=status_code, detail=detail) + + +class NotFoundError(AppException): + def __init__(self, resource: str, resource_id=None): + msg = f"{resource} not found" + if resource_id is not None: + msg = f"{resource} #{resource_id} not found" + super().__init__(msg, status.HTTP_404_NOT_FOUND) + + +class PermissionDeniedError(AppException): + def __init__(self, detail: str = "Permission denied"): + super().__init__(detail, status.HTTP_403_FORBIDDEN) + + +class TenantMismatchError(AppException): + def __init__(self): + super().__init__( + "Tenant mismatch: resource does not belong to your tenant", + status.HTTP_403_FORBIDDEN, + ) + + +class WorkerOfflineError(AppException): + def __init__(self, worker_name: str): + super().__init__( + f"AI Worker '{worker_name}' is offline or misconfigured", + status.HTTP_503_SERVICE_UNAVAILABLE, + ) + + +class BudgetExceededError(AppException): + def __init__(self, detail: str = "Budget limit exceeded"): + super().__init__(detail, status.HTTP_402_PAYMENT_REQUIRED) + + +class TaskNotExecutableError(AppException): + def __init__(self, detail: str = "Task cannot be executed in current state"): + super().__init__(detail, status.HTTP_409_CONFLICT)