RateLimitExceededException
Overview
RateLimitExceededException
is a custom exception class in the Midnite81\Guardian package. This exception is used to signal that a rate limit has been exceeded, particularly when interacting with external APIs or services within a Guardian-protected context.
Class Details
Namespace:
Midnite81\Guardian\Exceptions
Extends:
Exception
Purpose
The primary purpose of this exception is to allow developers to integrate external rate limiting information with Guardian's rate limiting system. When thrown inside a closure passed to Guardian's send()
method, it enables Guardian to manage rate limiting for external services consistently with its internal mechanisms.
Properties
protected DateTimeImmutable $retryAfter
: Stores the time when the client can retry the request.
Constructor
The constructor takes a $retryAfter
parameter, which can be:
A
DateTimeImmutable
objectAn integer (representing seconds)
A string (either an HTTP-date or seconds)
Methods
getRetryAfter
Returns a DateTimeImmutable
object representing when the client can retry the request.
Usage
This exception should be thrown by the user inside the closure passed to Guardian's send()
method, particularly when handling rate limits from external APIs or services.
Example:
Integration with Guardian
When this exception is thrown inside the closure passed to Guardian::send()
, Guardian will catch it and handle the rate limiting accordingly. This allows Guardian to manage rate limits for external services in the same way it manages internal rate limits.
Best Practices
Use this exception to wrap rate limit responses from external APIs or services.
Always include the 'Retry-After' information when throwing this exception.
Use descriptive error messages to distinguish between different rate limited services or endpoints.
Handle other types of exceptions appropriately within the closure.
Error Messages
The error message for this exception should be descriptive and indicate which service or API triggered the rate limit. For example:
Handling the Exception
Typically, you don't need to catch this exception yourself when using Guardian::send()
. Guardian will handle it internally. However, if you're using the exception outside of Guardian, you can catch and handle it like this:
Automatic Backoff Handling
When this exception is thrown inside a Guardian send()
method:
Guardian will catch the exception and record the rate limit violation.
Subsequent calls to
Guardian::send()
for the same identifier will be automatically prevented until the rate limit resets.You don't need to implement additional retry or backoff logic; Guardian handles this for you.
Security Considerations
By properly using this exception to signal rate limit violations from external services, you ensure that your application respects both internal and external rate limits. This prevents accidental abuse of services and helps maintain good relationships with API providers.
Remember, the key to effective use of RateLimitExceededException
is throwing it at the appropriate times within Guardian-protected closures, allowing Guardian to manage overall rate limiting behavior for your application.
Last updated