IBM Cloud Python SDK:如何在异常时检索错误消息?

IBM Cloud Python SDK: How to retrieve error message on exception?

我正在使用 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))

我收到一个错误,所以我想捕获异常并打印错误信息。我该怎么做?

我注意到 SDK GitHub 代码中的一些测试使用了 class ApiException provided by the IBM Python SDK Core。 class 具有属性 message。导入 ApiException 然后在错误打印消息时起作用。上面的代码现在是:

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)

您可以找到关于错误处理的更多信息 here。 link 是一个通用的“README”文档,该文档 link 从许多 IBM Cloud SDK 自述文件中编辑而来,并包含所有这些自述文件共有的使用信息。