errors.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. """errors and exceptions."""
  2. from __future__ import annotations
  3. from flask.wrappers import Response
  4. from werkzeug import exceptions
  5. from .wrappers import Limit
  6. class RateLimitExceeded(exceptions.TooManyRequests):
  7. """Exception raised when a rate limit is hit."""
  8. def __init__(self, limit: Limit, response: Response | None = None) -> None:
  9. """
  10. :param limit: The actual rate limit that was hit.
  11. Used to construct the default response message
  12. :param response: Optional pre constructed response. If provided
  13. it will be rendered by flask instead of the default error response
  14. of :class:`~werkzeug.exceptions.HTTPException`
  15. """
  16. self.limit = limit
  17. self.response = response
  18. if limit.error_message:
  19. description = (
  20. limit.error_message
  21. if not callable(limit.error_message)
  22. else limit.error_message()
  23. )
  24. else:
  25. description = str(limit.limit)
  26. super().__init__(description=description, response=response)