botocore.errorfactory.DBInstanceNotFoundFault 在 boto3

botocore.errorfactory.DBInstanceNotFoundFault in boto3

如何捕捉这个 boto 错误 botocore.errorfactory.DBInstanceNotFoundFault

这是我的 python3 代码:

import boto3
import botocore.exceptions
import botocore.errorfactory

clientrds = boto3.client('rds')

rdsInstances = ['prod-rds']


for rds in rdsInstances : 
    rds_response = clientrds.describe_db_instances(DBInstanceIdentifier=rds)
    DBInstanceStatus = (rds_response["DBInstances"][0]["DBInstanceStatus"])
    if DBInstanceStatus == "stopped" :
        print(rds," Switched On ")
    start_rds_response = clientrds.start_db_instance(DBInstanceIdentifier=rds)

控制台输出/错误:

Traceback (most recent call last):
  File "/home/rds.py", line 11, in <module>
    rds_response = clientrds.describe_db_instances(DBInstanceIdentifier=rds)
  File "/home/.local/lib/python3.8/site-packages/botocore/client.py", line 391, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/home/.local/lib/python3.8/site-packages/botocore/client.py", line 719, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.errorfactory.DBInstanceNotFoundFault: An error occurred (DBInstanceNotFound) when calling the DescribeDBInstances operation: DBInstance prod-rds not found.

可以使用botocore.exceptions

处理异常
for rds in rdsInstances : 
try :
    start_rds_response = clientrds.start_db_instance(DBInstanceIdentifier=rds)  #RDS.instance.start
except botocore.errorfactory.ClientError as error:
    if error.response['Error']['Code'] == 'DBInstanceNotFound': 
        print("DBInstanceNotFound")

参考:Link