Google API HTTP 异常处理不起作用 (python)
Google API HTTP exception handling doesn't work (python)
我正在尝试捕获 HTTP 错误。我使用了以下选项:
1) googleapiclient.errors.HttpError
2) google.api_core.exceptions.NotFound
这些选项都不适合我。任何想法为什么?谢谢!
from pprint import pprint
from googleapiclient import discovery, errors
from oauth2client.client import GoogleCredentials
#from google.api_core.exceptions import NotFound
credentials = GoogleCredentials.get_application_default()
service = discovery.build('compute', 'v1', credentials=credentials)
# Project ID for this request.
project = 'aweqwfnwrueifubwerfbiu' # TODO: Update placeholder value.
try:
request = service.firewalls().list(project=project)
except errors.HttpError:
pprint('Error!')
while request is not None:
response = request.execute()
for firewall in response['items']:
# TODO: Change code below to process each `firewall` resource:
pprint(firewall)
request = service.firewalls().list_next(previous_request=request, previous_response=response)
Traceback (most recent call last):
File "test.py", line 20, in <module>
response = request.execute()
File "/lib/python3.6/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/lib/python3.6/site-packages/googleapiclient/http.py", line 856, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://compute.googleapis.com/compute/v1/projects/aweqwfnwrueifubwerfbiu/global/firewalls?alt=json returned "The resource 'projects/aweqwfnwrueifubwerfbiu' was not found">
试试这个来处理错误
from googleapiclient.errors import HttpError
但是,错误似乎并没有出现在您的异常捕获所在的位置。就在这里
while request is not None:
response = request.execute()
这里没有
try:
request = service.firewalls().list(project=project)
except errors.HttpError:
pprint('Error!')
所以试着把它改成这个
while request is not None:
try:
response = request.execute()
except errors.HttpError:
pprint('Error!')
我正在尝试捕获 HTTP 错误。我使用了以下选项: 1) googleapiclient.errors.HttpError 2) google.api_core.exceptions.NotFound
这些选项都不适合我。任何想法为什么?谢谢!
from pprint import pprint
from googleapiclient import discovery, errors
from oauth2client.client import GoogleCredentials
#from google.api_core.exceptions import NotFound
credentials = GoogleCredentials.get_application_default()
service = discovery.build('compute', 'v1', credentials=credentials)
# Project ID for this request.
project = 'aweqwfnwrueifubwerfbiu' # TODO: Update placeholder value.
try:
request = service.firewalls().list(project=project)
except errors.HttpError:
pprint('Error!')
while request is not None:
response = request.execute()
for firewall in response['items']:
# TODO: Change code below to process each `firewall` resource:
pprint(firewall)
request = service.firewalls().list_next(previous_request=request, previous_response=response)
Traceback (most recent call last):
File "test.py", line 20, in <module>
response = request.execute()
File "/lib/python3.6/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/lib/python3.6/site-packages/googleapiclient/http.py", line 856, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://compute.googleapis.com/compute/v1/projects/aweqwfnwrueifubwerfbiu/global/firewalls?alt=json returned "The resource 'projects/aweqwfnwrueifubwerfbiu' was not found">
试试这个来处理错误
from googleapiclient.errors import HttpError
但是,错误似乎并没有出现在您的异常捕获所在的位置。就在这里
while request is not None:
response = request.execute()
这里没有
try:
request = service.firewalls().list(project=project)
except errors.HttpError:
pprint('Error!')
所以试着把它改成这个
while request is not None:
try:
response = request.execute()
except errors.HttpError:
pprint('Error!')