asweigart/ezsheets

KeyError during the exception handling when executing requests

solarpete opened this issue · 2 comments

Hi,
there seems to be a small bug in the following line of the exception handling when executing HTTP requests in the _makeRequest function:

if errorContent["error"]["errors"][0]["reason"] != "rateLimitExceeded":

When I use the EZSheets and reach the Google's quota after a lot of requests, I get the following error (which seems to be caused by non-existing key "errors" in the call on the line mentioned above):

Traceback (most recent call last):
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\ezsheets\__init__.py", line 146, in _makeRequest
    return request.execute()
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\googleapiclient\http.py", line 856, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 429 when requesting https://sheets.googleapis.com/v4/spreadsheets/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/values/XXXXXXXXXXXXXXX%21A1%3AH2?alt=json returned "
 'project_number:XXXXXXXXXXXX'.". Details: "[{'@type': 'type.googleapis.com/google.rpc.Help', 'links': [{'description': 'Google developer console API key', 'url': 'https://console.developers.google.com/project/XXXXXXXXXXXX/apiui/credential'}]}]">

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "download_compare_and_report_webpage.py", line 111, in <module>
    saveToGoogleSheets(googleSheetID, fileNameBasis, (dt.strftime('%d.%m.%Y'), dt.strftime('%H:%M:%S'), newFileName, previousFileName, 'Áno'), URL)
  File "download_compare_and_report_webpage.py", line 45, in saveToGoogleSheets
    sheet.rowCount += 1
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\ezsheets\__init__.py", line 590, in rowCount
    self.refresh() # Retrieve up-to-date grid properties from Google Sheets.
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\ezsheets\__init__.py", line 854, in refresh
    self._refreshData()
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\ezsheets\__init__.py", line 892, in _refreshData
    response = _makeRequest('values.get', **{'spreadsheetId': self._spreadsheet._spreadsheetId,
  File "C:\Users\Peter\AppData\Roaming\Python\Python38\site-packages\ezsheets\__init__.py", line 149, in _makeRequest
    if errorContent['error']['errors'][0]['reason'] != 'rateLimitExceeded':
KeyError: 'errors'

Note that I replaced what I thought could be confidential data with Xs.
Thank you and thanks for the good work with this project!

Merged this change. Thanks!

You're welcome and thanks for the merge and first and foremost for a perfect module!