Reputation: 352
I am using google-api-python-client library, provided by Google Cloud.
I am using lot of methods from it, to operate on compute-engine resources (Disks, instances, snapshots etc)
But while doing lot of operations, for better handling and resilient code, i should add try..catch
blocks & while adding the blocks, i realized that Google-Cloud doesn't provide any basic Exception class.
Worst case that i have thought of is:
try:
<code>
except:
<prompt error>
But i believe this isn't the right way to do.
Am i missing something ? or the exception handler isn't provided by GCP-Library?
Upvotes: 0
Views: 1756
Reputation: 352
I gone through the documentation and the Error stack that API returns & i was able to figure out the way to handle errors to handle exceptions in our code.
So, Unlike you mentioned, it is possible..!
Using following way, we could handle the errors/exceptions.
import oauth2client.client
from googleapiclient import discovery
import googleapiclient.errors
#
# prepare credentials dictionary
#
try:
compute = discovery.build(
"compute", "v1", credentials=credentials)
except oauth2client.client.HttpAccessTokenRefreshError as e:
print 'Error > ', e.message
except googleapiclient.errors.HttpError as e:
reason = str(e._get_reason).split("\"")[-2]
print 'Error > ', reason
I hope, this is helpful!
These two are the most common errors that come up. There might be other errors, which is completely dependent on case.
But all the errors should be present in googleapiclient.errors class
@Bill Prin, please let me know.
Upvotes: 3
Reputation: 2518
The short answer is that google-api-python-client
does not have the exception classes you are looking for.
The google-api-python-client
library is an older library that is supported but not actively developed. The client calling code is all autogenned and it's a very generic client for a lot of APIs so there aren't many meaningful exceptions coded in there other than at the network/transport/HTTP layer.
Development going forward is on google-cloud-python library, which is a mix of hand-written libraries and autogenerated ones. You may note that not all APIs, including compute, are there yet, although they will be in time. The new autogenerated code will focus more on idiomatic language patterns.
The new library has some more specific exceptions, but if you have any ideas on how to improve it further I'd describe your ideas as a Github issue on the google-cloud-python
client library.
Upvotes: 0