"""Domain exceptions. The API layer maps these to HTTP responses.""" class AppError(Exception): status_code = 500 public_message = "❌ Scanned images are not clear please retry!" code = "" # machine-readable reason for clients, e.g. "rate_limited" def __init__(self, detail: str = "", project_id: str = ""): super().__init__(detail or self.public_message) self.detail = detail self.project_id = project_id class InvalidUploadError(AppError): status_code = 400 public_message = "❌ Invalid upload" class PdfExtractionError(AppError): pass class PackagingError(AppError): pass class RateLimitedError(AppError): """The model provider refused the request with HTTP 429 (after the client's own retries).""" status_code = 429 public_message = "❌ The AI model is busy right now (rate limit reached). Please try again in a minute." code = "rate_limited" def is_rate_limit(exc: BaseException) -> bool: """True if exc, or any exception it was raised from, is a provider 429. Duck-typed so the API layer does not depend on the groq SDK (groq.RateLimitError has status_code 429). """ seen = set() while exc is not None and id(exc) not in seen: seen.add(id(exc)) if getattr(exc, "status_code", None) == 429 or type(exc).__name__ == "RateLimitError": return True exc = exc.__cause__ or exc.__context__ return False