Handling exceptions that occur in the API

761 views
Skip to first unread message

newmcc...@gmail.com

unread,
Feb 8, 2017, 10:28:31 PM2/8/17
to AdWords API Forum
Hello,

I'm using TargetingIdeaService from Adwords API. I'm trying to catch any exception happening during the requests, sleep for 31 seconds, then retry. However, it seems that there are some exceptions occurring in the library files that I wonder how can I handle them? Here is a snippet of my code which is intended to send a set of kewords and return some statistics such as CPC, search volume, etc.:

def getRequest(selector, targeting_idea_service):
 page
= targeting_idea_service.get(selector)
 
while True:
 try:
 # Get
 page = targeting_idea_service.get(selector)
 # Display results.
 if 'entries' in page:
 for result in page['entries']:
 attributes = {}
 for attribute in result['data']:
 attributes[attribute['key']] = getattr(attribute['value'], 'value', '0')
 #print result
 money = {}
 money = attributes['AVERAGE_CPC']
 cpc  = money['microAmount']
 out = ""
 out =  "{'" + str(attributes['KEYWORD_TEXT']) + "': {'CPC': " + str(cpc) + ", 'SV': " + str(attributes['SEARCH_VOLUME']) + ", 'CR': " + str(attributes['COMPETITION']) + "}}"
 print out
 else:
 print 'No related keywords were found.'
 except suds.WebFault as detail:
 print detail
 print "Sleeping WebFault"
 time.sleep(31)
 continue
 except Exception as e:
 print e
 print "Sleeping"
 time.sleep(31)          
 continue
 break
I'm using Python 2.7 and googleads API v. 5 and v. 4.7 with my test account

Here is the traceback for googleads version 5:

Traceback (most recent call last):

  File "cpc2.py", line 165, in <module>

    main(sys.argv[1:])

  File "cpc2.py", line 149, in main

    getRequest(selector, targeting_idea_service)

  File "cpc2.py", line 52, in getRequest

    page = targeting_idea_service.get(selector)

  File "/Library/Python/2.7/site-packages/googleads/common.py", line 700, in MakeSoapRequest

    *[_PackForSuds(arg, self.suds_client.factory) for arg in args])

  File "/Library/Python/2.7/site-packages/suds/client.py", line 521, in __call__

    return client.invoke(args, kwargs)

  File "/Library/Python/2.7/site-packages/suds/client.py", line 581, in invoke

    result = self.send(soapenv)

  File "/Library/Python/2.7/site-packages/suds/client.py", line 613, in send

    reply = self.options.transport.send(request)

  File "/Library/Python/2.7/site-packages/googleads/util.py", line 98, in PatchedHttpTransportSend

    fp = self.u2open(u2request)

  File "/Library/Python/2.7/site-packages/suds/transport/http.py", line 132, in u2open

    return url.open(u2request, timeout=tm)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 437, in open

    response = meth(req, response)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 550, in http_response

    'http', request, response, code, msg, hdrs)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 475, in error

    return self._call_chain(*args)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain

    result = func(*args)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 558, in http_error_default

    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)

urllib2.HTTPError: HTTP Error 500: Internal Server Error

Traceback for version 4.7:

Traceback (most recent call last):

  File "cpc2.py", line 165, in <module>

    main(sys.argv[1:])

  File "cpc2.py", line 149, in main

    getRequest(selector, targeting_idea_service)

  File "cpc2.py", line 52, in getRequest

    page = targeting_idea_service.get(selector)

  File "/Library/Python/2.7/site-packages/googleads/common.py", line 647, in MakeSoapRequest

    raise e

suds.WebFault: Server raised fault: '[RateExceededError <rateName=RATE_LIMIT, rateKey=null, rateScope=ACCOUNT, retryAfterSeconds=30>]'



Any advice in how to handle this exception in away that my function will sleep for a while then continue trying?

Thank you



Peter Oliquino

unread,
Feb 8, 2017, 11:09:25 PM2/8/17
to AdWords API Forum
Hi,

Based on the error message, you are experiencing a rate limit error at the account level. One possible cause is that you are making too many requests per second on a single account. This said, you may want to check the number of API calls being done at the same time against the account in question.

I also recommend that you refer to the tips mentioned here to minimize the number of request being made per second by your account. Additionally, you could use the exponential backoff policy when retrying your requests as stated in this section of our best practices guide. I hope this helps and please let me know if you encounter any other issues.

Best regards,
Peter
AdWords API Team

newm...@gmail.com

unread,
Feb 11, 2017, 1:28:30 AM2/11/17
to AdWords API Forum
I'm referring to the example of policy violation error, and tried to apply it to my case as follows: 
except suds.WebFault as e:
for error in e.fault.detail.ApiExceptionFault.errors:
if error['ApiError.Type'] == 'RateExceededError':
print "Sleeping RateExceededError"
time.sleep(31) 
continue 

Here is the error I'm having: 
TypeError: tuple indices must be integers, not str

and the variable error has the value of: ('fieldPath', )
Could you please advise me how to handle the exception correctly?  

I know that I need to retry according to suggested time returned in as a field in RateExceededError, but my concern know is know correct name of the exception to handle it   

Thank you

Joyce Lava

unread,
Feb 12, 2017, 10:33:55 PM2/12/17
to AdWords API Forum
Hi,

I will provide support to you on behalf of Peter. Could you please confirm if you tried to run the example code to get keyword ideas from the Python client library? I asked this because the error message you're encountering (TypeError: tuple indices must be integers, not str) seems not about the RateExceededError. It seems like it's on the type of field (e.g. Cpc which was from microAmount that has type long) to me. If you could try out the example code and send me (reply privately to author) the complete SOAP request and response, it will help me better check the issue further. 

With regards to the RateExceededError, it is encountered when you exceeded on a specific rate limit. It has different elements that will help you identify what kind of rate limit is exceeded. I hope this helps.

Regards,
Joyce, AdWords API Team
Reply all
Reply to author
Forward
0 new messages