我想import的exception是,当发生boto3 ssm不与发现的参数get_parameter.我正在尝试为库添加一些额外的ssm功能moto,但我在这一点上很难过.
>>> import boto3
>>> ssm = boto3.client('ssm')
>>> try:
ssm.get_parameter(Name='not_found')
except Exception as e:
print(type(e))
<class 'botocore.errorfactory.ParameterNotFound'>
>>> from botocore.errorfactory import ParameterNotFound
ImportError: cannot import name 'ParameterNotFound'
>>> import botocore.errorfactory.ParameterNotFound
ModuleNotFoundError: No module named 'botocore.errorfactory.ParameterNotFound'; 'botocore.errorfactory' is not a package
Run Code Online (Sandbox Code Playgroud)
但是,Exception无法导入,并且似乎不存在于botocore代码中.如何导入此例外?
我正在使用一个简单的 boto3 脚本从我的 aws 帐户中的 SSM 参数存储中检索参数。python 脚本如下所示:
client = get_boto3_client('ssm', 'us-east-1')
try:
response = client.get_parameter(Name='my_param_name',WithDecryption=True)
except Exception as e:
logging.error("retrieve param error: {0}".format(e))
raise e
return response
Run Code Online (Sandbox Code Playgroud)
如果给定的参数不可用,我会在响应中收到一个通用错误,如下所示:
An error occurred (ParameterNotFound) when calling the GetParameter operation: Parameter my_param_name not found.
Run Code Online (Sandbox Code Playgroud)
我已经验证了boto3 ssm docs 的方法签名。相关的AWS API 文档确认在参数存储中不存在参数时返回 400 响应。
我的问题是如何验证响应中捕获的异常是否实际上是 400 状态代码,以便我可以相应地处理它。