0
votes

I am using the IBM Cloud Platform Services Python SDK to work with access management tags. For that I am following the Python example to create a tag:

create_tag_results = global_tagging_service.create_tag(
  tag_names=['env:example-access-tag'],
  tag_type='access').get_result()

print(json.dumps(create_tag_results, indent=2))

I received an error, so I want to catch the exception and print the error message. How can I do that?

2

2 Answers

1
votes

I noticed that some tests in the SDK GitHub code use the class ApiException provided by the IBM Python SDK Core. That class has the attribute message. Importing ApiException and then on error printing the message works. The code from above is now:

from ibm_cloud_sdk_core import ApiException

...

try:
  create_tag_results = global_tagging.create_tag(
          tag_names=[tagname],
          tag_type=ttype).get_result()
  print(json.dumps(create_tag_results, indent=2))
except ApiException as e:
  print(e.message)
0
votes

You can find additional info re: error-handling here. That link is to a common "README" document that is linked from many of the IBM Cloud SDK README's and contains usage information that is common to all of them.