Initial commit: fastapi-traffic rate limiting library
- 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
This commit is contained in:
50
fastapi_traffic/exceptions.py
Normal file
50
fastapi_traffic/exceptions.py
Normal file
@@ -0,0 +1,50 @@
|
||||
"""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
|
||||
Reference in New Issue
Block a user