Firing off an HTTP request
Now that you have your Guardian instance, we can wrap our HTTP request to let Guardian handle any rate-limiting or error-handling rules we may have. We'll look at Cache Drivers and Rules in more detail in later sections.
This package doesn't limit how you fire your HTTP requests. You can write any code you'd like to run to get your data in a closure in the send
method. Let's assume for now the request is always successful.
Exception Handling
Guardian throws various exceptions to help you manage rate limiting and error scenarios. Here's how to handle them:
RulePreventsExecutionException
In our example above, we're hitting an API endpoint and getting some JSON back. However, our rules are rate-limiting our ability to get the data to only 6 requests per minute.
If we exceed this limit, Guardian will throw a RulePreventsExecutionException
, unless you specify false
as a second argument in the send
method, in which case it'll return null
.
Here's how to catch this exception:
RateLimitExceededException
There may be times when you set rules to prevent going over your rate limiting quota, but the server says you've already reached a limit. To prevent continued spamming of an API, we can throw a RateLimitExceededException
in the callback. Guardian will then prevent any further API calls until the expiry time has passed.
Store Exceptions
Guardian uses various storage mechanisms to keep track of rate limits and other data. Each storage mechanism can throw its own type of exception, all of which extend the base StoreException
. Here are the specific store exceptions:
DatabaseStoreException
: Thrown when there's an issue with database operations.FileStoreException
: Thrown when there's a problem with file-based storage operations.RedisStoreException
: Thrown when there's an issue with Redis operations.
You can catch these exceptions individually or catch the base StoreException
to handle all storage-related issues:
Other Exceptions
You should be prepared to catch any other exceptions that the callback may throw. A general catch (Exception $e)
is a good approach to prevent your application from potentially displaying an internal server error to users.
By handling these exceptions, you can gracefully manage rate limiting, storage issues, and other potential errors in your application.
Last updated