- Core rate limiting with multiple algorithms (sliding window, token bucket, etc.) - SQLite and memory backends - Decorator and dependency injection patterns - Middleware support - Example usage files
51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
"""Custom exceptions for FastAPI Traffic rate limiter."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from fastapi_traffic.core.models import RateLimitInfo
|
|
|
|
|
|
class FastAPITrafficError(Exception):
|
|
"""Base exception for all FastAPI Traffic errors."""
|
|
|
|
pass
|
|
|
|
|
|
class RateLimitExceeded(FastAPITrafficError):
|
|
"""Raised when a rate limit has been exceeded."""
|
|
|
|
def __init__(
|
|
self,
|
|
message: str = "Rate limit exceeded",
|
|
*,
|
|
retry_after: float | None = None,
|
|
limit_info: RateLimitInfo | None = None,
|
|
) -> None:
|
|
super().__init__(message)
|
|
self.message = message
|
|
self.retry_after = retry_after
|
|
self.limit_info = limit_info
|
|
|
|
|
|
class BackendError(FastAPITrafficError):
|
|
"""Raised when a backend operation fails."""
|
|
|
|
def __init__(
|
|
self,
|
|
message: str = "Backend operation failed",
|
|
*,
|
|
original_error: Exception | None = None,
|
|
) -> None:
|
|
super().__init__(message)
|
|
self.message = message
|
|
self.original_error = original_error
|
|
|
|
|
|
class ConfigurationError(FastAPITrafficError):
|
|
"""Raised when there is a configuration error."""
|
|
|
|
pass
|