Allow configuring the maximum ratelimit timeout before erroring

This is useful for cases where a rate limit is known to be
extraordinarily high, but you still want to handle the error.
This is common with routes such as emoji creation.
This commit is contained in:
Rapptz
2022-07-18 23:56:38 -04:00
parent 85ea418776
commit 76402b00f9
4 changed files with 79 additions and 11 deletions

View File

@ -38,6 +38,7 @@ __all__ = (
'ClientException',
'GatewayNotFound',
'HTTPException',
'RateLimited',
'Forbidden',
'NotFound',
'DiscordServerError',
@ -137,6 +138,30 @@ class HTTPException(DiscordException):
super().__init__(fmt.format(self.response, self.code, self.text))
class RateLimited(DiscordException):
"""Exception that's raised for when status code 429 occurs
and the timeout is greater than the configured maximum using
the ``max_ratelimit_timeout`` parameter in :class:`Client`.
This is not raised during global ratelimits.
Since sometimes requests are halted pre-emptively before they're
even made, **this does not subclass :exc:`HTTPException`.**
.. versionadded:: 2.0
Attributes
------------
retry_after: :class:`float`
The amount of seconds that the client should wait before retrying
the request.
"""
def __init__(self, retry_after: float):
self.retry_after = retry_after
super().__init__(f'Too many requests. Retry in {retry_after:.2f} seconds.')
class Forbidden(HTTPException):
"""Exception that's raised for when status code 403 occurs.