Example Code for Handling RateExceededError

102 views
Skip to first unread message

Nick Harris

unread,
Apr 28, 2017, 9:48:42 PM4/28/17
to AdWords API Forum
I want to be able to properly handle RateExceededError as described in https://developers.google.com/adwords/api/docs/guides/rate-limits.

Are there any code samples that show how to do this?

Any way to simulate a RateExceededError so I can make sure my app handles it correctly? Or should I just launch thousands of concurrent API calls to generate a real error?

Nick Harris

unread,
Apr 30, 2017, 11:40:39 PM4/30/17
to AdWords API Forum
In process of answering my own question. Using a test account, I set up an infinite AWQL query loop that runs until it triggers a RATE_EXCEEDED error, like so:
while(2 > 1) {
    try {
        $page = CampaignService->query('SELECT id, .....);
    } catch (ApiException $apiException) {
        
        foreach ($apiException->getErrors() as $error) {
            $errorReason = $error->getReason();
            if($errorReason == "RATE_EXCEEDED") {
                $errArr = ([
                    'reason'            => $error->getReason(),
                    'rateName'          => $error->getRateName(),
                    'rateScope'         => $error->getRateScope(),
                    'retryAfterSeconds' => $error->getRetryAfterSeconds()
                ]);
                die(var_dump($errArr));
            }
        }
    }
}

That fetches all the info there is about a RATE_EXCEEDED error. Next step is actually handle the retryAfterSeconds with further API calls...

Vishal Vinayak (Adwords API Team)

unread,
May 1, 2017, 12:08:26 PM5/1/17
to AdWords API Forum
Hi,

You can also check out the procedure mentioned in this example and check if the error is an instance of RateExceededError and perform action in your code accordingly. Although their is no direct way to simulate RateExceededError in your API call, you can try to throw the RateExceededError manually in your code for that purpose. Also, an additional resource, although available in Java programming language only,  is the RateLimiter extension which can help handle rate limiting related issues by changing a few lines of code. Please revert if you have additional questions or concerns. 

Regards,
Vishal, AdWords API Team

Nick Harris

unread,
May 1, 2017, 10:17:06 PM5/1/17
to AdWords API Forum
Thanks Vishal, followed your advice and am throwing a RateExceededError that replicates what a real one looks like using the following code:

$rateExceededError = new \Google\AdsApi\AdWords\v201702\cm\RateExceededError("", null, "","RateExceededError.RATE_EXCEEDED", "RateExceededError", "RATE_EXCEEDED","RequestsPerMinute", "ACCOUNT", 30);
        
$apiException = new \Google\AdsApi\AdWords\v201702\cm\ApiException(null, null, null, [$rateExceededError]);

// Now do something with $apiException
Reply all
Reply to author
Forward
0 new messages